Hello All,I have to write the SP to cursor through all the user tables in all user databases.Here is my code:DECLARE database_cursor CURSORFOR SELECT name FROM master..sysdatabasesWHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')FOR READ onlyOPEN database_cursor FETCH next FROM database_cursor INTO @dbname WHILE @@FETCH_STATUS = 0 BEGIN --checking fragmentation in each table-- Declare cursorDECLARE alltables CURSOR FORSELECT convert(varchar,so.id)FROM + '''+ @dbname +''' + .. + sysobjects soJOIN sysindexes siON so.id = si.idWHERE so.type ='U'AND si.indid < 2AND si.rows > 0OPEN alltables-- Loop through all the tables in the database running dbcc showcontig on each oneFETCH NEXTFROM alltablesINTO @tableidchar
I get an error at the first cursor where I pass @dbname. Can anyone tell me how to correct the error?Thanks,-S