SQL Server:你能帮我完成这个查询吗?

发布于 2024-08-31 19:15:33 字数 1650 浏览 13 评论 0原文

我想在我们的 SQL Server 2008 数据库服务器上运行诊断报告。

我循环遍历所有数据库,然后对于每个数据库,我想查看每个表。但是,当我查看每个表(使用 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

I want to run a diagnostic report on our SQL Server 2008 database server.

I am looping through all of the databases, and then for each database, I want to look at each table. But, when I go to look at each table (with tbl_cursor), it always picks up the tables in the database 'master'.

I think it's because of my tbl_cursor selection :

    SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'

How do I fix this?


Here's the entire code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-09-07 19:15:34

问题是因为您实际上总是在主数据库上下文下运行 INFORMATION_SCHEMA.TABLES 查询。

您需要将 tbl_cursor 块转换为动态 SQL,以便使用数据库名称完全限定查询。

例如

SELECT table_name FROM YourDatabase.INFORMATION_SCHEMA.TABLES WHERE....

本质上是您需要为该光标执行的操作。

The problem is because you're actually always running the INFORMATION_SCHEMA.TABLES query under the master db context.

You'd need to convert the tbl_cursor block into dynamic SQL in order to fully qualify the query with the DB name.

e.g.

SELECT table_name FROM YourDatabase.INFORMATION_SCHEMA.TABLES WHERE....

is essentially what you need to be executing for that cursor.

甜心小果奶 2024-09-07 19:15:34

使用表变量更容易,因此您可以使用另一个动态 SQL 语句将行添加到 @tablist

SET NOCOUNT ON

DECLARE @table_count INT
DECLARE @dblist TABLE (DBName VARCHAR(100))
DECLARE @tablist TABLE (TableName VARCHAR(100))
DECLARE @dbname varchar(100), @tabname varchar(100)

INSERT @dblist 
SELECT name FROM sys.databases where name<>N'master'

SELECT TOP 1 @dbname = DBName FROM @dblist
WHILE @@ROWCOUNT <> 0
BEGIN

    INSERT @tablist (tableName)
    EXEC ('SELECT table_name FROM ' + @dbname + '.information_schema.tables WHERE table_type = ''base table'' ')

    SELECT TOP 1 @tabname = tableName FROM @tablist
    WHILE @@ROWCOUNT <> 0
    BEGIN

--do my stuff

        DELETE @tablist WHERE tableName =  @tabname
        SELECT TOP 1 @tabname = tableName FROM @tablist
    END

    DELETE @dblist WHERE DBName =  @dbname
    SELECT TOP 1 @dbname = DBName FROM @dblist
END

It's easier to use table variables so you can add rows to @tablist using another dynamic SQL statement

SET NOCOUNT ON

DECLARE @table_count INT
DECLARE @dblist TABLE (DBName VARCHAR(100))
DECLARE @tablist TABLE (TableName VARCHAR(100))
DECLARE @dbname varchar(100), @tabname varchar(100)

INSERT @dblist 
SELECT name FROM sys.databases where name<>N'master'

SELECT TOP 1 @dbname = DBName FROM @dblist
WHILE @@ROWCOUNT <> 0
BEGIN

    INSERT @tablist (tableName)
    EXEC ('SELECT table_name FROM ' + @dbname + '.information_schema.tables WHERE table_type = ''base table'' ')

    SELECT TOP 1 @tabname = tableName FROM @tablist
    WHILE @@ROWCOUNT <> 0
    BEGIN

--do my stuff

        DELETE @tablist WHERE tableName =  @tabname
        SELECT TOP 1 @tabname = tableName FROM @tablist
    END

    DELETE @dblist WHERE DBName =  @dbname
    SELECT TOP 1 @dbname = DBName FROM @dblist
END
痕至 2024-09-07 19:15:34

您可能必须创建动态 SQL。因为 information_schema 将仅从您运行此查询的当前活动数据库中获取对象。

你可以尝试 sys.objects

You might have to create dynamic SQL. because information_schema will fetch objects only from the current active database against which you are running this query.

you can try sys.objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文