I know there are a number of posts regarding the topic but I would like you to provide a solution to run the following (rebuild indexes) query on multiple databases, each with the same structure and hosted in the same instance.
The query is:
DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
I can run this manually and it works but there are 180 databases on this instance.