Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DECLARE @dbName NVARCHAR(255)
- DECLARE @sqlCommand NVARCHAR(MAX)
- -- Cursor to iterate over each user database
- DECLARE db_cursor CURSOR FOR
- SELECT name
- FROM sys.databases
- WHERE state_desc = 'ONLINE'
- AND name NOT IN ('master', 'tempdb', 'model', 'msdb')
- AND name NOT LIKE 'PRIEMPRE%'
- AND name LIKE 'PRI%'
- -- Exclude system databases
- OPEN db_cursor
- FETCH NEXT FROM db_cursor INTO @dbName
- WHILE @@FETCH_STATUS = 0
- BEGIN
- SET @sqlCommand = '
- IF EXISTS (
- SELECT 1
- FROM ' + QUOTENAME(@dbName) + '.sys.tables AS t
- INNER JOIN ' + QUOTENAME(@dbName) + '.sys.schemas AS s
- ON t.schema_id = s.schema_id
- WHERE t.name = ''Movimentos''
- AND s.name = ''dbo''
- )
- BEGIN
- USE ' + QUOTENAME(@dbName) + ';
- CREATE NONCLUSTERED INDEX IX_Movimentos_Utilizador
- ON dbo.Movimentos (DataGravacao)
- INCLUDE (Utilizador);
- END'
- -- Print the command for debugging purposes (optional)
- --PRINT @sqlCommand
- -- Execute the dynamic SQL command
- EXEC sp_executesql @sqlCommand
- FETCH NEXT FROM db_cursor INTO @dbName
- END
- CLOSE db_cursor
- DEALLOCATE db_cursor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement