DECLARE @SQL nvarchar(4000)
DECLARE @message nvarchar(255)
DECLARE @name nvarchar(255)
DECLARE @dbContext nvarchar(256)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM sys.databases
WHERE name LIKE 'EskulapWin%'
ORDER BY name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @message = 'Database: ' + @name
PRINT @message
SET @SQL = 'USE ' + @name + CHAR(13) + '
IF EXISTS (SELECT * FROM ' + @name + '.sys.schemas WHERE name = ''Ims'')
DROP SCHEMA [Ims]'
print @SQL
exec sp_executesql @SQL
SET @SQL = 'USE ' + @name + CHAR(13) + '
IF EXISTS (SELECT * FROM ' + @name + '.sys.database_principals WHERE name = ''Ims'')
DROP USER [Ims]'
print @SQL
exec sp_executesql @SQL
SET @SQL = 'USE ' + @name + CHAR(13) + 'CREATE USER [Ims] FOR LOGIN [Ims]'
PRINT @SQL
SET @dbContext = @name + '.dbo.' + 'sp_executeSQL'
EXEC @dbContext @SQL
SET @SQL = 'USE ' + @name + CHAR(13) + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_Bridge_ListAll TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_History_ListByPeriod TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_History_Update TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_History_GetLastSuccess TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_Product_ListAll TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_SellIn_ListByPeriod TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_SellOut_ListByPeriod TO Ims ' + CHAR(10)
SET @SQL = @SQL + 'GRANT EXECUTE ON '+ @name+ '.dbo.Ims_Stock_ListAll TO Ims ' + CHAR(10)
PRINT @SQL
EXEC sp_executesql @SQL
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor