SQL Iterate Over All Tables

后端 未结 4 2000
生来不讨喜
生来不讨喜 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:50

    Please try this dynamic sql. remove comment from exec when you are ready to run

    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)
    

提交回复
热议问题