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

sql 查询的问题
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)


declare   @a   int
set   @a   =   1
select   column1   from   mytable
我想当@a=1时只查询column1,当@a=2只查询column2,当@a=3时查询2列。我不想用动态的sql语句,因为真实的查询已经写好了,改成动态很困难。     请帮忙看一下

------解决方案--------------------
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)


declare @a int
set @a = 1
select (Case @a When 1 Then column1 When 2 Then column2 End) As [column] from mytable

Drop Table mytable