if EXISTS(select * from where 字段1='X' and 字段2='Y')
begin
........
end

解决方案 »

  1.   

    if EXISTS(select * from where 字段1='X' and 字段2='Y')
      

  2.   

    if exists (select 1 from table where col1='x' and col2='y')
      print 'true'
    else
      print 'false'
      

  3.   

    if Exists(select 字段2 from Table1  where 字段1='X' and  字段2<>'Y')
    return false
      

  4.   

    If Exists (Select * from Table1 where 字段1='X' and 字段2 !='Y')
      Return false
    else
      Return True
      

  5.   

    if not EXISTS(select * from where 字段1='X' and 字段2<>'Y')
    begin
    ........
    end
      

  6.   

    在TABLE1中要求查找满足以下条件的记录:
    1/字段1='X'
    2/如所有满足条件1的记录中,字段2都是'Y',返回TRUE;字段2有一个不是'Y',返回FALSE用IF EXISTS(),一个SQL语句完成If exists(select * from Table1 where 字段1='X' and 字段2='Y')
    print 'true'
    else
    print 'false'
      

  7.   

    可能我没把题意说清楚.举几个测试用例:
    字段1     字段2      应该返回值
    X          Y      *
    X          Y      *     TRUE _____1
    X          Y      *
    --*********************************
    X          Y      *
    X          Y      *     FALSE_____2
    X          Z      *
    --*********************************
    Z          Y      *
    Z          Y      *     FALSE_____3
    Z          Y      *
    --*********************************
    Z          Y      *
    Z          Y      *     FALSE_____4
    Z          Z      *If exists(select * from Table1 where 字段1='X' and 字段2='Y')  违反用例2
    if not EXISTS(select * from where 字段1='X' and 字段2<>'Y')  违反用例3\4if Exists(select 字段2 from Table1  where 字段1='X' and  字段2<>'Y')
    return false                          符合用例2,逻辑上正确,但是必须再测试用例3\4才能确保所有返回FALSE值的情况都已被考虑实际上,我只想找出用例1的情况,用IF EXISTS(),返回TRUE值即可.说的不对处,请各位指点.
      

  8.   

    If not exists(select 1 from Table1 where 字段1<>'X' or 字段2<>'Y')
      

  9.   

    If not exists(select 1 from Table1 where 字段1<>'X' or (字段2<>'Y' and 字段1='X'))
      

  10.   

    谢谢大力.
    1、恕我钻牛角尖,如果一定要用If exists,不能用If not exists,有办法吗?
    2、如果用If not exists(select 1 from Table1 where 字段1<>'X' or (字段2<>'Y' and 字段1='X'))的句型:
       将原来的条件(字段1='X') 替换为(字段1='X' and 字段3='m' and 字段4='n'),语句怎么写?  要穷举所有的反面条件不成?头都大了!
      

  11.   

    1、恕我钻牛角尖,如果一定要用If exists,不能用If not exists,有办法吗?If (select count(1) from 表 where 字段1='X')=(select count(1) from 表 where 字段1='X' and 字段2='Y')
      

  12.   

    if  exists ( select * from t where (col1='x' and col2<>'y') or col1<>'x')
      print 'false'
    else
      print 'true'
      

  13.   

    楼上不行,如:
    --*********************************
    X          Y      *
    X          Y      *     FALSE_____2
    X          Z      *
      

  14.   

    If exists(select * from Table1 where 字段1='X' and 字段2<>'Y')
    begin
    print 'true'
    end
    else
    begin
    print 'false'
    end
      

  15.   

    如果按这样的思路,能不能写一个语句:
    1、做一个子查询,(SELECT * FROM TABLE1 where 字段1='X');
    2、如果这样的子查询存在,检查子查询中是否字段2都是'Y'(NOT EXISTS(字段2<>'Y')?); 
    3、返回TRUE
      

  16.   

    not exists (SELECT * FROM TABLE1 where 字段1='X' and 字段2<>'Y')不就可以了?
      

  17.   

    if exists(select * into #temp from table1 where field1='X' and (exists (select * from #temp where field2<>'Y')))
      print 'false'
    else
      print 'true'