我想运行类似这样的查询:
if exists (...)
    begin
        select * from T1
        select * from T2
    end
else
    begin
        ...
    end
为什么exists条件为真时只有select * from T1被执行?我想当条件满足时运行多条语句该怎么解决?

解决方案 »

  1.   

    --都執行Y
    if object_id('ta')is not null drop table ta
    go
    create table ta(ID int)
    insert ta select 1 union all select 2
    if object_id('tb')is not null drop table tb
    go
    create table tb(ID int)
    insert tb select 1 union all select 2
    if exists(select 1 from ta)
      begin
       select * from ta
       select * from tb
      end
    /*ID          
    ----------- 
    1
    2(影響 2 個資料列)ID          
    ----------- 
    1
    2(影響 2 個資料列)*/
      

  2.   

    begin end 就是语句块。
    应该是select * from T1 都执行
         select * from T2 
    。查看T2表中是否有数据?