我是想查询一个字段下面所有的值 比如
table a
-----------------------------------------------
     b
1    1
2    1
3    1
4    0
-----------------------------------------------
如果B列值都为1,返回Y,如果还有别的值,返回X

解决方案 »

  1.   

    select case b when '1' then 'Y' else 'X' end from tablename
      

  2.   

    declare @t table(a int,b int)insert @t 
    select 1,1 union all
    select 2,1 union all
    select 3,1 union all
    select 4,0
    select b=case when exists(select 1 from @t where b<>1) then 'X' else 'Y' endb    
    ---- 
    X(所影响的行数为 1 行)
      

  3.   

    happydreamer(重返csdn) 不对,他问的是所有B的值,而不是每行B的值
      

  4.   


    --新建一个函数判断列中是否都是'1'
    if object_id('FN_ckStr')>0
    drop function FN_ckStr
    gocreate function FN_ckStr(@str varchar(8000))
    returns int
    begin
      declare @i int
      declare @re int
    set @str = replace(@str,' ','')
      set @i  = 1
    set @re = 1  while @i<len(@str)
      begin
    if substring(@str,@i,1) <> '1'
      begin
           set @re = 0
     break
    end
    set @i = @i + 1
      end return @re
    enddeclare @a table(b varchar(30))
    insert into @a
    select '1 1' union all
    select '2 1' union all
    select '3 1' union all
    select '4 0' select case dbo.FN_ckStr(b) when 1 then 'Y' else 'N' end as b
    from @a
      

  5.   

    select case dbo.FN_ckStr(b) when 1 then 'Y' else 'X' end as b
    from @a
      

  6.   


    (所影响的行数为 4 行)b    
    ---- 
    Y
    X
    X
    X(所影响的行数为 4 行)
      

  7.   

    多谢各位关心! dawugui(潇洒老乌龟) 先生说得对,我问是所有B的值。只要返回一个X或Y就好了。
    我调试下,少候结帖。
      

  8.   

    if exists (select * from tablename where  b!=1)
    select X
    else  select Y
      

  9.   

    潇洒的老乌龟,你写错了个字段
    select b=case when exists(select b from @t where b<>1) then 'X' else 'Y' end
                                     此处是1而不是b
      

  10.   

    潇洒的老乌龟,你写错了个字段
    select b=case when exists(select b from @t where b<>1) then 'X' else 'Y' end
                                     此处是b而不是1SORRY:刚才写错了