这个有测试数据:--> 测试数据: @t1
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select (SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21) from @t1 t1
(所影响的行数为 2 行)
(所影响的行数为 1 行)     
---- 

NULL    ------这里是NULL,不是否(所影响的行数为 2 行)
1,为什么结果会是NULL,不是“否”
2,怎样能让结果变成“否”

解决方案 »

  1.   

    --> 测试数据: @t1
    declare @t1 table (c11 int,c12 int)
    insert into @t1
    select 1,11 union all
    select 2,22
    --> 测试数据: @t2
    declare @t2 table (c21 int,c22 int)
    insert into @t2
    select 1,111select '是' from @t1 t1 where exists (select 1 from @t2 t2 where t2.c21 = t1.c11)
    union all
    select '否' from @t1 t1 where not exists (select 1 from @t2 t2 where t2.c21 = t1.c11)/*
         
    ---- 

    否(所影响的行数为 2 行)
    */
      

  2.   

    select 
    case 
     when (SELECT CASE WHEN c22 FROM @t2 where t1.c11 = c21) IS NULL 
       THEN '否' 
     ELSE '是' 
    END 
     from @t1 t1
      

  3.   

    select case when ((select c22  FROM @t2 where t1.c11 = c21))is null then '否' else '是' end 
    from @t1 t1
      

  4.   

    select isnull(SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21),'否')
    from @t1 t1
      

  5.   

    --> 测试数据: @t1
    declare @t1 table (c11 int,c12 int)
    insert into @t1
    select 1,11 union all
    select 2,22
    --> 测试数据: @t2
    declare @t2 table (c21 int,c22 int)
    insert into @t2
    select 1,111select CASE WHEN (SELECT  c22 FROM @t2 where t1.c11 = c21) IS NULL THEN '否' ELSE '是' END from @t1 t1
    、*----

    否(2 行受影响)
    *、
      

  6.   

    select CASE WHEN (SELECT c22 FROM @t2 where c21=t1.c11) IS NULL THEN '否' ELSE'是' end  from @t1 t1
      

  7.   

    --> 测试数据: @t1
    declare @t1 table (c11 int,c12 int)
    insert into @t1
    select 1,11 union all
    select 2,22
    --> 测试数据: @t2
    declare @t2 table (c21 int,c22 int)
    insert into @t2
    select 1,111select m.* , case when c22 is null then '否' else '是' end 
    from @t1 m left join @t2 n on m.c11 = n.c21/*
     c11         c12              
    ----------- ----------- ---- 
    1           11          是
    2           22          否(所影响的行数为 2 行)*/
      

  8.   

    --> 测试数据: @t1
    declare @t1 table (c11 int,c12 int)
    insert into @t1
    select 1,11 union all
    select 2,22
    --> 测试数据: @t2
    declare @t2 table (c21 int,c22 int)
    insert into @t2
    select 1,111select isnull((SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21),'否')
    from @t1 t1
    /*----

    否(2 行受影响)
    */
      

  9.   

    SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END 
    FROM @t2 Right JOIN @t1 t1 ON t1.c11 = c21

    /*
    ----

    否(2 行受影响)*/
      

  10.   

    select 
    case when b.c21 is null then '否' else '是' end
    from @t1 a
    left join @t2 b on a.c11=b.c21