DECLARE @query VARCHAR(100), @dbname sysname
DECLARE BadCursor CURSOR FOR
SELECT name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb')
OPEN BadCursor
FETCH NEXT FROM BadCursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @query = 'dbcc checkdb(' + quotename(@dbname) + ')'
EXEC (@query)
FETCH NEXT FROM BadCursor INTO @dbname
END
CLOSE BadCursor
DEALLOCATE BadCursor
GO
Noch ein kleines Beispiel für das Erstellen einer Tabelle.
DECLARE @query VARCHAR(100),
@dbname sysname
DECLARE @dblist TABLE(dbname sysname)
INSERT INTO @dblist(dbname) SELECT name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb')
WHILE (SELECT count(*) FROM @dblist) > 0
BEGIN
SELECT top 1 @dbname = dbname FROM @dblist
SELECT @query = 'dbcc checkdb(' + quotename(@dbname) + ')'
EXEC(@query)
DELETE FROM @dblist WHERE dbname = @dbname
END
GO