declare @str varcahr(100)='select id' if(select col1 from tb where id=@id) is not null set @str=@str+',col1' if(select col2 from tb where id=@id) is not null set @str=@str+',col3' ... exec(@str+' where id='''+@id+'')
declare @str varchar(100)='select id', @id int=1; if(select col1 from ccc where id=@id) is not null set @str=@str+',col1' if(select col2 from ccc where id=@id) is not null set @str=@str+',col2' if(select col3 from ccc where id=@id) is not null set @str=@str+',col3' --print @str exec(@str+' from ccc where id='''+@id+'''')
if(select col1 from tb where id=@id) is not null
set @str=@str+',col1'
if(select col2 from tb where id=@id) is not null
set @str=@str+',col3'
...
exec(@str+' where id='''+@id+'')
@id int=1;
if(select col1 from ccc where id=@id) is not null
set @str=@str+',col1'
if(select col2 from ccc where id=@id) is not null
set @str=@str+',col2'
if(select col3 from ccc where id=@id) is not null
set @str=@str+',col3'
--print @str
exec(@str+' from ccc where id='''+@id+'''')