简体   繁体   English

SQL遍历所有表

[英]SQL Iterate Over All Tables

I am running the following code to extract all relevant rows from all tables that have a particular column. 我正在运行以下代码,从具有特定列的所有表中提取所有相关行。 The outer IF is supposed to check if the column exists on the table for that iteration. 外部IF应该检查该迭代表是否存在该列。 If not, it should finish that iteration and move to the next table. 如果没有,它应该完成该迭代并移至下一张表。 If the table has the GCRecord column, it should then check to see if that table will return any records. 如果该表具有GCRecord列,则应检查该表是否将返回任何记录。 If there are no records to return, it should end that iteration and move on to the next table. 如果没有要返回的记录,则应结束该迭代并移至下一个表。 If there are records, it should display them in SSMS. 如果有记录,则应在SSMS中显示它们。

USE WS_Live

EXECUTE sp_MSforeachtable 
'
    USE WS_Live

    IF EXISTS(  SELECT * 
                FROM sys.columns
                WHERE columns.Object_ID = Object_ID(''?'')
                AND Name = ''GCRecord''
             ) 
    BEGIN
        IF EXISTS (SELECT * FROM ? WHERE GCRecord IS NOT NULL)

        BEGIN
            SELECT * FROM ? WHERE GCRecord IS NOT NULL
        END
    END
'

It seems to work because SSMS is only returning grids with valid entries. 这似乎可行,因为SSMS仅返回带有有效条目的网格。 What I don't understand is: Why am I still getting these errors? 我不明白的是:为什么我仍然会收到这些错误?

Msg 207, Level 16, State 1, Line 10
Invalid column name 'GCRecord'.
Msg 207, Level 16, State 1, Line 13
Invalid column name 'GCRecord'.

EDIT 编辑

After using the suggestions, I have this: 使用建议后,我有以下内容:

USE WS_Live

EXECUTE sp_MSforeachtable 
'
    USE WS_Live

    IF EXISTS(SELECT * FROM sys.columns WHERE columns.Object_ID = Object_ID(''?'')AND Name = ''GCRecord'') 

    BEGIN
    IF EXISTS (SELECT * FROM ? WHERE GCRecord IS NOT NULL)

            BEGIN
               EXEC('' SELECT * FROM ? WHERE GCRecord IS NOT NULL'')
            END
    END
'

Which returns this error: 哪个返回此错误:

Msg 207, Level 16, State 1, Line 7
Invalid column name 'GCRecord'.

Which refers to this line 指这条线

IF EXISTS(SELECT * FROM sys.columns WHERE columns.Object_ID = Object_ID(''?'')AND Name = ''GCRecord'') 

UPDATE 更新

I tried nesting EXEC statements which did not work, but using the selected answer I got the results I was looking for without the errors. 我尝试嵌套无效的EXEC语句,但是使用选择的答案,我得到的结果是我所期望的而没有错误。

Use Dynamic query inside begin to avoid inner pre-compilation of code, for tables do not contain column ' GCRecord ' 开始使用动态查询,以避免内部预先编译代码,因为表不包含列“ GCRecord

USE WS_Live
GO

EXECUTE sp_MSforeachtable 
'
    IF EXISTS(  SELECT * 
                FROM sys.columns
                WHERE columns.Object_ID = Object_ID(''?'')
                AND Name = ''GCRecord''
             ) 
    BEGIN

          EXEC(''          
                  IF EXISTS (SELECT * FROM ? WHERE GCRecord IS NOT NULL)
                 BEGIN
                         SELECT * FROM ? WHERE GCRecord IS NOT NULL
                 END
              '')
    END
'

You are very close. 你很亲密 Use "EXEC" 使用“ EXEC”

USE WS_Live 

EXECUTE sp_MSforeachtable  
' 
    USE WS_Live 

    IF EXISTS(  SELECT *  
                FROM sys.columns 
                WHERE columns.Object_ID = Object_ID(''?'') 
                AND Name = ''GCRecord''
             ) 
    BEGIN
        EXEC(''SELECT * FROM ? WHERE GCRecord IS NOT NULL'')
    END
'

Please try this dynamic sql. 请尝试此动态sql。 remove comment from exec when you are ready to run 准备好运行时从exec删除注释

declare @t varchar(max) = ''

SELECT @t =  @t + 'SELECT * FROM ' + a.name + ' WHERE GCRecord IS NOT NULL;' + char(13)
                FROM sys.columns b join sys.objects a  on
                 b.Object_ID = a.Object_ID
                WHERE b.Name ='CreateDt'

Print  @t
--exec (@t)

Use @whereand to simplify: 使用@whereand来简化:

exec sp_MSforeachtable 
@command1='select * from ? where GCRecord is not null', 
@whereand='and exists(select 1 from sys.columns c where c.object_id = o.id 
                      and c.name = ''GCRecord'')' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM