SQL Iterate Over All Tables

后端 未结 4 1957
生来不讨喜
生来不讨喜 2021-01-24 07:13

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 o

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-24 07:43

    Use @whereand to simplify:

    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'')' 
    

提交回复
热议问题