oracle中select语句

qq_37967593 2017-03-19 10:31:26
实现功能:如果表2为空就查表一,否则查询表1
if exists (select 0 from table2)
select * from table 1
else
select * from table 2
这样的语句实现不了,在oracle中该怎么写
...全文
833 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
金木犀 2018-02-02
  • 打赏
  • 举报
回复

话说,虽然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;


qq_28538637 2017-04-24
  • 打赏
  • 举报
回复

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;
qq_28538637 2017-04-24
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/392138410#new_post
牛尚小又何妨 2017-04-23
  • 打赏
  • 举报
回复
这帖子写得很仔细啊 http://blog.csdn.net/hollboy/article/details/7550171
qq_28538637 2017-04-21
  • 打赏
  • 举报
回复
引用 7 楼 gaojiagang 的回复:
这样可以直接查出来的

select * from DEPT2
UNION
select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
小伙子
zbdzjx 2017-04-21
  • 打赏
  • 举报
回复
引用 8 楼 qq_28538637 的回复:
[quote=引用 7 楼 gaojiagang 的回复:] 这样可以直接查出来的

select * from DEPT2
UNION
select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
小伙子[/quote] 表结构不同不行吧。如果表结构相同,是没问题。
雨巷啊 2017-04-20
  • 打赏
  • 举报
回复
这样可以直接查出来的

select * from DEPT2
UNION
select * from DEPT WHERE 0 = (select COUNT(*) from DEPT2)
  • 打赏
  • 举报
回复
引用 5 楼 qq_37252813 的回复:
end;
ORACLE 好像不能在 块里面直接用 select 的吧 貌似必须要 into
ACE-2017 2017-04-19
  • 打赏
  • 举报
回复
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;
卖水果的net 2017-03-19
  • 打赏
  • 举报
回复
declare v_count int; begin select count(*) into v_count from t1; if v_count > 0 then 语句1 else 语句2 end if; end;
卖水果的net 2017-03-19
  • 打赏
  • 举报
回复
declare v_count int; begin select count(*) into v_count from t1; if v_count > 0 then 语句1 else 语句2 end; end;
califord 2017-03-19
  • 打赏
  • 举报
回复
实现功能:如果表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 告诉你的是思路,具体的代码你要根据你的程序改一下
你看见了吗° 2017-03-19
  • 打赏
  • 举报
回复
引用 2 楼 wmxcn2000 的回复:
declare v_count int; begin select count(*) into v_count from t1; if v_count > 0 then 语句1 else 语句2 end if; end;
赞同,楼主你要先确定表2为空,就可以用比如ID计数count的方式(最好用一定不会为空的字段),查询后如果count=0就说明为空,大于0就不为空咯

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧