【问题标题】:If exist then update in oracle forms 11g如果存在,则在 oracle 表单 11g 中更新
【发布时间】:2020-10-20 18:07:50
【问题描述】:

我正在尝试编写一个代码块,如果记录已经存在则插入记录然后更新 桌子。我正在尝试If (sql%rowcount = 0) then,但它在游标和多个记录中不起作用。

到目前为止,我尝试过的代码块是

declare
  remp_id     varchar2(60);
  remp_name   varchar2(100);
  rdesig      varchar2(100);
  rdept_no    number;
  rdesig_no   number;
  rdept_name  varchar2(60);
  cursor alfa is
    select emp_code, emp_name, desig, dept_name, dept_no, desig_no
      from emp
     where emp_code between :first_code and :second_code;
begin
  open alfa;
  loop
    fetch alfa
      into remp_id, remp_name, rdesig, rdept_name, rdept_no, rdesig_no;
    exit when alfa%notfound;
    update att_reg_mo
       set emp_code  = remp_id,
           emp_name  = remp_name,
           desig     = rdesig,
           dept_name = rdept_name,
           dept_no   = rdept_no,
           desig_no  = rdesig_no,
           att_date  = :att_date,
           emp_att   = :emp_att,
           att_type  = 'MA',
           reg_date  = :reg_date
     where emp_code between :first_code and :second_code
       and reg_date = :reg_date
       and att_date = :att_date;
    commit;
    if (sql%rowcount = 0) then
      insert into att_reg_mo
        (emp_code,
         emp_name,
         desig,
         dept_name,
         att_date,
         emp_att,
         att_type,
         reg_date,
         dept_no,
         desig_no)
      values
        (remp_id,
         remp_name,
         rdesig,
         rdept_name,
         :att_date,
         :emp_att,
         'MA',
         :reg_date,
         rdept_no,
         rdesig_no);
    end if;
    commit;
  end loop;
  close alfa;
end;

当我触发触发器时,记录被插入,但在需要更新记录的地方,它使用空值更新

【问题讨论】:

    标签: oracle plsql oracle11g


    【解决方案1】:

    或者你可以使用类似的东西:

    DECLARE
      cursor test is 
            select 1 as v from dual
            union 
            select 2 as v from dual;
      n_var NUMBER;
    BEGIN
      for rec in test loop
      BEGIN
          select 1 into n_var from dual where rec.v=2;
          DBMS_OUTPUT.PUT_LINE('Please Update Any Table');
          EXCEPTION
                WHEN no_data_found THEN
                  DBMS_OUTPUT.PUT_LINE('Please Insert Any Table');
      END;
      end loop;
    EXCEPTION
          WHEN OTHERS THEN
              DBMS_OUTPUT.PUT_LINE ('Unexpected error');
    END;
    

    【讨论】:

    • 如何在光标中使用它?我得到错误'错误#103遇到以下符号“异常”(开始case声明并退出goto if loop mod null ....)'
    【解决方案2】:

    SQL%attribute 总是指最近运行的SELECTDML 语句。对于假定 AUTOCOMMIT 的会话,在发出任何事务语句(例如 COMMITROLLBACKSAVEPOINT)后刷新为从 开始关闭 默认。因此,您总是会从 INSERT 语句之前的 SQL%ROWCOUNT 得到零,并在每次运行代码块期间不断插入到相关表中。

    因此,请从您的代码块中删除 f i r s t COMMIT删除还会保留整个事务的原子性

    Demo

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      相关资源
      最近更新 更多