information_schema.columns
でチェックする必要がある列名を持つテーブル変数を作成します。次に、テーブル変数とinformation_schema.columns
の間のチェックを行います。これがどれほど効率的かはわかりません。
クエリ
declare @cols as varchar(max) = 'UpdatedDate,CreatedDate,UpdatedBy';
declare @cols2 as varchar(max) = '(' + char(39);
set @cols2 += replace(@cols, ',', '''),(''') + ''');';
declare @sql as varchar(max) = 'declare @tbl as table([col] varchar(1000));';
set @sql += 'insert into @tbl values' + @cols2;
set @sql += 'declare @tot as int;set @tot = (select count(*) from @tbl);';
set @sql += 'select case when count(*) = @tot
then ''true'' else ''false'' end as [status]
from @tbl t1 where exists(
select 1 from information_schema.columns t2
where t1.[col] = t2.[column_name]
and t2.[table_name] = ''MyTable'');';
exec(@sql);