[转]Oracle中没有 if exists(...)
本文转自:http://blog.csdn.net/hollboy/article/details/7550171
对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种
第一种是最常用的,判断count(*)的值是否为零,如下
declare
v_cnt number;
begin
select count(*) into v_cnt from T_VIP where col=1;
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
首先这种写法让人感觉很奇怪,明明只需要知道表里有没有记录,却去统计了全表的记录数。 这种方式对于小表而言可以接受,一旦表记录很多的时候,性能问题就非常严重 因此有人就作了些修改,改成 select count(*) into v_cnt from T_VIP where col=1 and rownum=1 看起来似乎解决了性能问题,但是分析执行计划可以知道,实际上是一样的,不推荐使用。
第二种是所谓进攻式编程,不作预先判断,而是直接默认通过判断,然后使用 exception 来捕获异常 比如我这里不判断表中是否有满足条件的记录,默认它有,如果没有就在异常中进行处理
declare
v_1 number;
begin
select vip_level into v_1 from T_VIP where 1=0;
exception
when no_data_found then
dbms_output.put_line('无记录');
end;
这种方式从性能上讲比第一种要好得多 不过首先它没办法适应所有的情况,如第一段代码它就没办法改造 其次这种代码看起来让人觉得好像是发生了异常,而不是正常运行,从而造成混乱,不推荐使用。
第三种是利用 Oracle 原有的 Exists 语法,如下
declare v_cnt number; begin select count(*) into v_cnt from dual where exists (select * from t_vip where col=1); if v_cnt = 0 then dbms_output.put_line('无记录'); end if; end;
declare
v_cnt number;
begin
select count(*)
into v_cnt
from dual
where exists (select * from t_vip where col=1);
if v_cnt = 0 then
dbms_output.put_line('无记录');
end if;
end;
通过在语句的外面套上一层dual,来使用oracle原有的exists语法 虽然和第一种看起来类似,但分析执行计划可以知道,性能比以上两种都要好得多,与MSSQL的 if exists 最接近,推荐使用。
可以把判断封装成一个函数以方便使用,代码如下
CREATE OR REPLACE FUNCTION EXISTS2 (IN_SQL IN VARCHAR2)
RETURN NUMBER
IS
/**********************************************************
* 使用示例
* begin
* if EXISTS2('select * from dual where 1=1')=1 then
* dbms_output.put_line('有记录');
* else
* dbms_output.put_line('无记录');
* end if;
* end;
*****************************************************************/
V_SQL VARCHAR2(4000);
V_CNT NUMBER(1);
BEGIN
V_SQL := 'SELECT COUNT(*) FROM DUAL WHERE EXISTS (' || IN_SQL || ')';
EXECUTE IMMEDIATE V_SQL INTO V_CNT;
RETURN(V_CNT);
END;
对于常用的insert判断还有更简单的写法,比如以下代码
if not exists(select * from table1 where id=1) insert into table1 values(1,'a');
可以改写成 insert when (not exists(select * from table1 where id=1)) then into table1 select 1 as id, 'a' as data from dual;
-
再比如以下的代码 if not exists(select * from table1 where id=2) insert into table1 values(2,'b') else update table1 set data='b' where id=2;
可以改写成
merge into table1 his
using
(
select 2 as id, 'b' as data from dual
) src
on (his.id=src.id)
when matched then
update set his.data=src.data where id=src.id
when not matched then
insert values(src.id,src.data);
这里附带说下,有人喜欢把count(*)写成count(列名),不推荐后一种,因为列名是需要额外的操作,去查询系统表来定位列信息
另外count(1)和count(*)没有差别,推荐使用count(*)直观明了
[转]Oracle中没有 if exists(...)的更多相关文章
- Oracle中没有 if exists(...)
对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种 第一种是最常用的,判断count(*)的值是否为零,如下declare v ...
- ORACLE 中IN和EXISTS比较
ORACLE 中IN和EXISTS比较 EXISTS的执行流程 select * from t1 where exists ( select null from t2 where y = x ...
- Oracle中没有 if exists(...)的解决方法
http://blog.csdn.net/hollboy/article/details/7550171对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常 ...
- 【转】oracle中in和exists的区别
原文地址:http://blog.itpub.net/7478833/viewspace-441043/ 感谢作者 in 和 exists区别 in 是把外表和内表作hash join,而ex ...
- Oracle中in和exists的选择
在ORACLE 11G大行其道的今天,还有很多人受早期版本的影响,记住一些既定的规则, 1.子查询结果集小,用IN 2.外表小,子查询表大,用EXISTS 摘自:http://blog.chi ...
- oracle中in与exists的区别
exists是用来判断是否存在的,当exists中的查询存在结果时则返回真,否则返回假.not exists则相反. exists做为where 条件时,是先对where 前的主查询询进行查询,然后用 ...
- 关于oracle中in和exists的区别
一般来说,这两个是用来做两张(或更多)表联合查询用的,in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,假设有A.B两个表,使用时是这样的: 1.select * from ...
- Oracle中 in、exists、not in,not exists的比较
最基本的区别: in 对主表使用索引 exists 对子表使用索引 not in 不使用索引 not exists 对主子表都使用索引 写法: exist的where条件是: "...... ...
- oracle中in和exists的区别
IN适合于外表大而内表小的情况:EXISTS适合于外表小而内表大的情况. 性能上的比较 比如Select * from T1 where x in ( select y from T2 ) 执行的过程 ...
随机推荐
- windows下gitbook与开源中国码云关联,以及如何gitbook转pdf
gitbook能够很方便的和github关联,实现团队协作的效果.可是github私有库需要付费.但是开源中国码云能够建私有库,于是考虑将gitbook关联码云,折腾了一番后,能够可视化的关联,后面就 ...
- Winform Log4net 配置写不同文件
以下配置了二种写文件,第一种根据日期写文件yyyyMMdd.txt,第二种是写固定文件login.txt. 1, 下载Log4net组件: http://logging.apache.org/log4 ...
- UML uml建模工具
UML建模工具 一.UML的概念: 参考百度百科: https://baike.baidu.com/item/%E7%BB%9F%E4%B8%80%E5%BB%BA%E6%A8%A1%E8%AF%AD ...
- Java的进程内缓存框架:EhCache
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种 ...
- WDF(Windows Driver Frameworks)驱动框架源码!!
微软官方提供源码:https://github.com/Microsoft/Windows-Driver-Frameworks
- 【bzoj 3595】: [Scoi2014]方伯伯的Oj
传送门&& 原题解 蒟蒻终于做到一道方伯伯的题了…… 调了一个上午一直TLE(发现自己打了好久的splay板子竟然是错的这种丢人事情我就不说了) 很明显,要建两棵树,$T1$维护排名, ...
- php获取随机字符串的几种方法
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...
- luogu4074 [WC2013]糖果公园(树上带修莫队)
link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...
- 如何在NSDocumentDirectory内新建一个文件夹
iOS下载文件一般保存到NSDocumentDirectory内,但是为了更好整理文件内容,那就要自定义的生成一些文件夹,和做一些删除文件夹的操作. - (NSString *)pathToPatie ...
- CSS 两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行解决办法
我们可以发现:两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行,这是什么问题呢? 我们先来看一下效果: <!DOCTYPE html> <html lang= ...