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;
通过在语句的外面套上一层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(...)的解决方法的更多相关文章

  1. Oracle中没有 if exists(...)

    对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种 第一种是最常用的,判断count(*)的值是否为零,如下declare  v ...

  2. [转]Oracle中没有 if exists(...)

    本文转自:http://blog.csdn.net/hollboy/article/details/7550171 对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法, ...

  3. linux下安装Oracle时交换空间不足的解决方法

    摘:linux下安装Oracle时交换空间不足的解决方法 linux上安装Oracle时交换空间不足的解决办法 增加交换空间有两种方法: 严格的说,在系统安装完后只有一种方法可以增加swap,那就是本 ...

  4. VS2012中丢失ArcGIS模板的解决方法

    VS2012中丢失ArcGIS模板的解决方法 由于ArcGIS10.0(for .NET)默认是用VS2010作为开发工具的,所以在先安装VS2012后装ArcGIS10.0 桌面版及ArcObjec ...

  5. js中style.display=""无效的解决方法

    本文实例讲述了js中style.display=""无效的解决方法.分享给大家供大家参考.具体解决方法如下: 一.问题描述: 在js中我们有时想动态的控制一个div显示或隐藏或更多 ...

  6. java开发中遇到的问题及解决方法(持续更新)

    摘自 http://blog.csdn.net/pony12/article/details/38456261 java开发中遇到的问题及解决方法(持续更新) 工作中,以C/C++开发为主,难免与其他 ...

  7. ORACLE 中IN和EXISTS比较

    ORACLE 中IN和EXISTS比较 EXISTS的执行流程      select * from t1 where exists ( select null from t2 where y = x ...

  8. navicat连接oracle数据库报ORA-28547: connection to server failed, probable Oracle Net admin error错误的解决方法

    原文:navicat连接oracle数据库报ORA-28547: connection to server failed, probable Oracle Net admin error错误的解决方法 ...

  9. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

随机推荐

  1. 【SPOJ694】Distinct Substrings (SA)

    求不相同子串个数    该问题等价于求所有后缀间不相同前缀的个数..也就是对于每个后缀suffix(sa[i]),将贡献出n-sa[i]+1个,但同时,要减去那些重复的,即为height[i],故答案 ...

  2. BZOJ3720 Gty的妹子树 【树分块】

    题目 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕-- Gty神(xian)犇(chong)从来不缺妹子-- 他来到了一棵妹子树下,发现每个妹子有一个美丽度 ...

  3. python(4)-- 日期 & 时间

    1. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 2. 时间间隔是以秒为单位的浮点小数. 3. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长 ...

  4. node.js express配置允许跨域

    app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*& ...

  5. asp.net 字符串替换、截取。

    有时候要在一段字符串里面把某些字符替换成其他字符,怎么办? 例如: string image=@"csks/news/user_top/qqqq/qqqq.jpg"; image ...

  6. poj 2280 Islands and Bridges 哈密尔顿路 状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求一条哈密尔顿路径\(C_1C_2...C_n\),使其\(value\)最大. \(value\)的计算方式如下:\[\begin{aligned}v ...

  7. hdu 4528(搜索好题)

    小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

  8. LeetCode OJ-- Surrounded Regions **@

    https://oj.leetcode.com/problems/surrounded-regions/ 棋盘类的题目.找出所有没有被 X 包围的 O 使用深搜,但是太深了,run time erro ...

  9. 【转载】SQL SERVER 函数大全

    SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...

  10. Codeforces Gym101063 F.Bandejao (2016 USP-ICMC)

    F.Bandejao It is lunch time on Mars! Everyone has got that big smile on their faces, all eager to se ...