源地址:http://zhidao.baidu.com/question/340437545.html?seed=0
无意当中看到这个问题,自己没什么好的方法,故求大神。
8 个解决方案
#1
以前讨论过这个 用动态拼接。
#2
哦 怎么写?? 求链接
#3
笨办法就是拼字符串,其实你可以在程序里动态绑定数据控件
#4
我只想知道sql是怎么写 在程序里实现谁都会
#5
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+'')
#6
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+'''')
@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+'''')
#7
谢谢 2位 学习了
结贴
结贴
#8
嗯 不错 不错
#1
以前讨论过这个 用动态拼接。
#2
哦 怎么写?? 求链接
#3
笨办法就是拼字符串,其实你可以在程序里动态绑定数据控件
#4
我只想知道sql是怎么写 在程序里实现谁都会
#5
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+'')
#6
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+'''')
@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+'''')
#7
谢谢 2位 学习了
结贴
结贴
#8
嗯 不错 不错