实现功能:如果表2为空就查表一,否则查询表1
if exists (select 0 from table2)
select * from table 1
else
select * from table 2
这样的语句实现不了,在oracle中该怎么写

解决方案 »

  1.   

    declare v_count int;
    begin
    select count(*) into v_count from t1;if v_count > 0 then
    语句1
    else
    语句2
    end;
    end;
      

  2.   

    declare v_count int;
    begin
    select count(*) into v_count from t1;if v_count > 0 then
    语句1
    else
    语句2
    end if;
    end;
      

  3.   

    赞同,楼主你要先确定表2为空,就可以用比如ID计数count的方式(最好用一定不会为空的字段),查询后如果count=0就说明为空,大于0就不为空咯
      

  4.   

    实现功能:如果表2为空就查表一,否则查询表1????   那不都是查表一吗??估计是实现功能:如果表2为空就查表一,否则查询表2用存储过程可以实现先是定义一个变量
    cnt  int   --就是整形的变量cnt
    --然后查询表2是不是为空
    select  count(*)  into cnt  from table2
    --最后根据上面的查询结果判断查询哪张表
    if  cnt>0  then 
        select * form table2
    else
       select * form table1
    告诉你的是思路,具体的代码你要根据你的程序改一下
      

  5.   

    declare v_count int;
    begin
    select count(*) into v_count from t1;if v_count > 0 then
    select * from table 1
    else
    select * from table 2
    end if;
    end;
      

  6.   

     ORACLE 好像不能在 块里面直接用 select 的吧  貌似必须要 into
      

  7.   

    这样可以直接查出来的
    select * from DEPT2
    UNION
    select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
      

  8.   

    这帖子写得很仔细啊
    http://blog.csdn.net/hollboy/article/details/7550171
      

  9.   

    http://bbs.csdn.net/topics/392138410#new_post
      

  10.   


    DECLARE
      v_tmp    NUMBER := 0;
      v_result NUMBER;
    BEGIN
      SELECT COUNT(* ) INTO v_tmp FROM student;
      IF v_tmp <= 0 THEN
        dbms_output.put_line('execute table2');
      ELSE
        dbms_output.put_line('execute table1');
      END IF;
    END;
      

  11.   


    话说,虽然Oracle没有if exists(...)
    但是我能可以这样写declare v_count int;
    begin
    select count(*) into v_count from t1;if v_count > 0 then
    select * from table 1
    else
    select * from table 2
    end if;
    end;