【问题标题】:PL/SQL- How to insert into a table using all columns of a cursorPL/SQL-如何使用游标的所有列插入表
【发布时间】:2017-10-20 07:16:15
【问题描述】:

我正在使用游标在表中插入数据,因为如果记录失败,我只想丢弃该记录并继续插入其余记录。

所以我使用光标来检索信息。

有没有办法一次插入光标的所有列而不是一一选择?

 cursor c1 is 
select a,b,c,d,e from ab where a = 'something';

begin
for var_c1 in c1  loop 
begin

insert into ba (a,b,c,d,e) 
values (var_c1.all);

-- instead of values (var_c1.a, var_c1.b, var_c1.c,var_c1.d, var_c1.e) 
exception when others then continue;
end;

end;

【问题讨论】:

标签: oracle for-loop plsql insert cursor


【解决方案1】:

为了提高性能,您应该将所有记录放入一个集合中,然后您可以使用 BULK INSERT... SAVE EXCEPTION,如下所示:

DECLARE
  TYPE t_tab IS TABLE OF ba%ROWTYPE;

  l_tab          t_tab := t_tab();
  l_error_count  NUMBER;

  ex_dml_errors EXCEPTION;
  PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);
BEGIN
  -- Fill the collection.  ***
  -- l_tab  <--- ??
  -- *** next question is to make l_tab fill with the result of your cursor    
  --
  -- Perform a bulk operation.
  BEGIN
    FORALL i IN l_tab.first .. l_tab.last SAVE EXCEPTIONS
      INSERT INTO ba
      VALUES l_tab(i);
  EXCEPTION
    WHEN ex_dml_errors THEN
      l_error_count := SQL%BULK_EXCEPTIONS.count;
      DBMS_OUTPUT.put_line('Number of failures: ' || l_error_count);
      FOR i IN 1 .. l_error_count LOOP
        DBMS_OUTPUT.put_line('Error: ' || i || 
          ' Array Index: ' || SQL%BULK_EXCEPTIONS(i).error_index ||
          ' Message: ' || SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
      END LOOP;
  END;
END;
/

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多