日期:2014-05-19  浏览次数:20522 次

请帮我写一个这样的查询
根据参数赋值的不同,查询不同的条件,例如:
变量status有两种不同的值:Y和T,
如果是Y,则查询条件为:col1=status,
如果是T,则查询条件为:   col2=status.

------解决方案--------------------
变量status有两种不同的值:Y和T,
如果是Y,则查询条件为:col1=status,
如果是T,则查询条件为: col2=status.

查哪个表?
------解决方案--------------------
declare @status varchar(10),@sql varchar(1000)
set @status= 'Y '
select @sql= 'select * from 表 where '+case when @status= 'Y ' then 'clo1= ' when @status= 'T ' then 'col2= ' end + 'status '
print @sql
------解决方案--------------------
exec(@sql)
------解决方案--------------------
select case when status = 'Y ' then col1 else col2 end from t1
------解决方案--------------------
select * from table
where status=
(select case when status = 'Y ' then col1 else col2 end from table)