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

sql 查询问题2
create   table   mytable(column1   int,column2   int)
insert   into   mytable   values(1,1)
insert   into   mytable   values(2,2)
insert   into   mytable   values(3,3)
insert   into   mytable   values(4,4)
insert   into   mytable   values(5,5)

select   *   from   mytable
declare   @a   varchar(10)
set   @a   =   'True '
select     column1,column2   from   mytable

Drop   Table   mytable

我想@a   =   'True '的时候才将column1选出来。不用动态sql语句。不用

declare   @a   varchar(10)
if   (@a   =   'True ')
select     column1,column2   from   mytable
else
select       column2   from   mytable
的方式。(因为那会将我的存储过程变的很长)。可不可以用case   when   语句实现


------解决方案--------------------
如果要分成两个列不行,如果合并成字符串一列可以实现
------解决方案--------------------
select column1,column2 from mytable where 1=case when @a= 'True ' then 1 else 0 end
------解决方案--------------------
case when ?不行
或如果是查询用函数
------解决方案--------------------
你這樣的情況,用case沒辦法