实现功能:如果表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 告诉你的是思路,具体的代码你要根据你的程序改一下
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;
ORACLE 好像不能在 块里面直接用 select 的吧 貌似必须要 into
这样可以直接查出来的 select * from DEPT2 UNION select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
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;
话说,虽然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;
begin
select count(*) into v_count from t1;if v_count > 0 then
语句1
else
语句2
end;
end;
begin
select count(*) into v_count from t1;if v_count > 0 then
语句1
else
语句2
end if;
end;
cnt int --就是整形的变量cnt
--然后查询表2是不是为空
select count(*) into cnt from table2
--最后根据上面的查询结果判断查询哪张表
if cnt>0 then
select * form table2
else
select * form table1
告诉你的是思路,具体的代码你要根据你的程序改一下
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;
select * from DEPT2
UNION
select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
http://blog.csdn.net/hollboy/article/details/7550171
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;
话说,虽然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;