Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Cursor through sysdatabases

Author  Topic 

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-04-29 : 16:02:41
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 CURSOR
FOR SELECT name FROM master..sysdatabases
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')
FOR READ only

OPEN database_cursor
FETCH next FROM database_cursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN

--checking fragmentation in each table
-- Declare cursor
DECLARE alltables CURSOR FOR
SELECT convert(varchar,so.id)
FROM + '''+ @dbname +''' + .. + sysobjects so
JOIN sysindexes si
ON so.id = si.id
WHERE so.type ='U'
AND si.indid < 2
AND si.rows > 0
OPEN alltables

-- Loop through all the tables in the database running dbcc showcontig on each one
FETCH NEXT
FROM alltables
INTO @tableidchar


I get an error at the first cursor where I pass @dbname. Can anyone tell me how to correct the error?

Thanks,
-S

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-29 : 16:04:47
If you are using SQL Server 2005, then you should not be using DBCC SHOWCONTIG.

From BOL:
quote:

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use sys.dm_db_index_physical_stats instead.



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-04-29 : 16:06:27
I use SQL Server 2000
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-29 : 16:10:17
It's confusing to post a SQL Server 2000 question in a SQL Server 2005 forum. I am moving your thread to a more appropriate forum.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2008-04-29 : 16:13:10
Sorry. Thanks for moving it under sql 2000
Go to Top of Page
   

- Advertisement -