--好像不会,你是不是弄错了。
declare @re varchar(10)
if exists(select 1 from authors where 1=2
union 
select 1 from jobs where 1>2
union 
select 1 from jobs

set @re= 'ok'
else 
set @re= 'er'
print @re

解决方案 »

  1.   

    --楼主说的情况不可能。请仔细检查原代码。
    declare @Rlt intdeclare @t table(id int)if exists(select * from @t union select 1)
    set @Rlt = 4
    else 
    set @Rlt = 5
    print @Rlt
    /*
    结果
    4
    */
      

  2.   

    你把你的语句提出来先测试一下呢?如果测试通过了,然后你再把它带到你的if exists里吧
      

  3.   

    先不判EXISTS,就执行一下UNION起来的SELECT,看有没有记录啊
      

  4.   

    我说的这段是放在sp执行,大家说的,copy出来单独执行,和我在sp的结果是一样的!只要3个一起,就认为not exists,把前面2个select 用/**/起来,就是exists,还真是头痛~~~~
    哦,我同事帮我搞定了,发现了sql server7.0的一个bug,
    他只认识前面2个union,2000.0就没有问题,测试用wgsasd311的代码!1.在7.0的server:
    declare @re varchar(10)
    if exists(select 1 from prg_sys where 1=2
    union 
    select 1 from prg_sys  where 1>2
    union 
    select 1 from prg_sys

    set @re= 'ok'
    else 
    set @re= 'er'
    print @re/*
    er
    */2.declare @re varchar(10)if exists(
    select 1 from prg_sys
    union
    select 1 from prg_sys where 1=2 --放在第一个或者第2个就是ok
    union 
    select 1 from prg_sys  where 1>2

    set @re= 'ok'
    else 
    set @re= 'er'
    print @re/*
    ok
    */我狂晕,不够还好,我分开检查就可以啦!
    大家如果有用7.0,就记住这个bug,否则,掉了n根头发,也想不出来:.(
    谢谢
      

  5.   

    將union 改為 union all就OK了
      

  6.   

    楼上的,你有试过吗?用union 或union all都是一样滴
      

  7.   

    to  MorningTea(一勺抹茶):
    你的问题在WINDOWS2000里是没问题的,从我上午发给你的测试语句可以得知,但在7.0就不行,也许像你说的一样,7.0版本最多支持两层的union (union all)嵌套.可以在7.0 里测试下以下语句查下:
    select 1 as col
    union 
    select 2
    union 
    select 3
    union 
    select 4
      

  8.   

    用union 或union all不是一样的吧,UNION指定组合多个结果集并将其作为单个结果集返回。UNION ALL在结果中包含所有的行,包括重复行。如果没有指定,则删除重复行。
      

  9.   

    Dear All:     我的问题准确的说是因为SQL SERVER的底版本原因,与操作系统没有干系,因为2台SQL SERVER在不同电脑上,并且都是WINDOWS 2000 SERVER,我只是在我的电脑用客户端的查询编译器执行代码测试的!
         正如wgsasd311说的7.0的IF EXISTS只能识别两层的union (union all)嵌套,但是iwl()的担心没有必要,因为他的确UNION ,UNION ALL是不同,UNION 会删除重复列,7.0也是如此,你知道吗?我的3个SELECT只有一个有记录,并且只有一条,所以用UNION 或者UNION ALL没有影响!这里是因为IF EXISTS的BUG原因啦!
         TKS!
      

  10.   

    SQL Server 7.0 Service Pack 4
    http://www.microsoft.com/sql/downloads/sp4.mspx