我想在我们的SQL Server 2008数据库服务器上运行诊断报告。SQL Server:你能帮我解决这个问题吗?
我正在循环所有的数据库,然后为每个数据库,我想看看每个表。但是,当我去看每张桌子(与tbl_cursor
)时,它总是拿起数据库'master'
中的表格。
我想这是因为我的tbl_cursor
选择:
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'
我该如何解决这个问题?
这里是整个代码:
SET NOCOUNT ON
DECLARE @table_count INT
DECLARE @db_cursor VARCHAR(100)
DECLARE database_cursor CURSOR FOR
SELECT name FROM sys.databases where name<>N'master'
OPEN database_cursor
FETCH NEXT FROM database_cursor INTO @db_cursor
WHILE @@Fetch_status = 0
BEGIN
PRINT @db_cursor
SET @table_count = 0
DECLARE @table_cursor VARCHAR(100)
DECLARE tbl_cursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'
OPEN tbl_cursor
FETCH NEXT FROM tbl_cursor INTO @table_cursor
WHILE @@Fetch_status = 0
BEGIN
DECLARE @table_cmd NVARCHAR(255)
SET @table_cmd = N'IF NOT EXISTS(SELECT TOP(1) * FROM ' + @table_cursor + ') PRINT N'' Table ''''' + @table_cursor + ''''' is empty'' '
--PRINT @table_cmd --debug
EXEC sp_executesql @table_cmd
SET @table_count = @table_count + 1
FETCH NEXT FROM tbl_cursor INTO @table_cursor
END
CLOSE tbl_cursor
DEALLOCATE tbl_cursor
PRINT @db_cursor + N' Total Tables : ' + CAST(@table_count as varchar(2))
PRINT N'' -- print another blank line
SET @table_count = 0
FETCH NEXT FROM database_cursor INTO @db_cursor
END
CLOSE database_cursor
DEALLOCATE database_cursor
SET NOCOUNT OFF
@rib:没有“MS SQL”这样的东西。该产品被称为“SQL Server”。 “MS SQL”可能太容易与“MySQL”混淆。 – 2010-05-11 19:22:01