select test from test where test=@test@test有三个值"all","a","b"
我想当@test=all的时候选取所有数据,当@test=a的时候只选取test=a的数据,b同理.
不用if test='all' 然后怎样怎样这种判断能实现吗?新手,还请指教.谢谢.

解决方案 »

  1.   

    ---all好像比较难处理。declare @s varchar(20)
    Set @s = 'a'
    select * from test 
    where test =
    case when @s = 'x' then @s 
    when @s = 'y' then @s
    else '' end 
      

  2.   

    select test 
    from test 
    where test=case when @test='all' then test else @test end
      

  3.   

    select test from test 
    where test=@test or @test='all'
      

  4.   

    DECLARE @tb table(id int,val varchar(50));
    INSERT @tb SELECT 1,'a'
    UNION ALL SELECT 2,'b'
    UNION ALL SELECT 3,'c'
    UNION ALL SELECT 4,'a'
    UNION ALL SELECT 5,'b'
    UNION ALL SELECT 6,'d'DECLARE @s varchar(10);
    SET @s = 'a';SELECT * FROM @tb WHERE val = CASE WHEN @s = 'all' THEN val ELSE @s END
      

  5.   


    select a.test from test a where a.test=(select case when @test='all' then a.test else
    @test end )
      

  6.   

    create table aaa
    (a int,test char(10));
    go
    insert into aaa
    select 1,'a'
    union all
    select 2,'b'
    union all
    select 3,'all'
    go
    declare @test char(10)
    set @test='all'
    select a.test from aaa a where a.test=(select case when @test='all' then a.test else
    @test end )
    2008测试通过
      

  7.   

    哦,原来还可以这样。之前没想过这样的写法。declare @test varchar(20) 
    set @test = 'all'
    select * from test where test= (case when @test ='x' then @test when @test='y' then @test else test end)