zoukankan      html  css  js  c++  java
  • Oracle中没有 if exists(...)

    对于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(*)直观明了


  • 相关阅读:
    模拟两位选手进行n羽毛球比赛(15分赛制)并计算模拟胜率
    Pyton实例
    Python图片处理
    jieba库的使用和好玩的词云
    Python汉诺塔问题
    多线程同时操作一个epoll_fd
    Linux tr命令
    iptables 深入分析
    Linux xtables
    Linux IPC 共享内存
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4083322.html
Copyright © 2011-2022 走看看