使用批量更新运行时插入,出现内部错误?

发布于 2024-08-31 09:31:10 字数 955 浏览 7 评论 0原文

我正在尝试创建一个名为动态的运行时表,并使用批量更新从索引按表将数据插入其中,但是当我尝试执行它时,出现了此错误:

第 1 行出现错误: ORA-06550: 第 0 行,第 0 列: PLS-00801:内部错误[74301

]

declare

     type index_tbl_type IS table of
        number
     index by binary_integer;
     num_tbl index_tbl_type;
     TYPE ref_cur IS REF CURSOR;
     cur_emp ref_cur;
    begin
         execute immediate 'create table dynamic (v_num number)';--Creating a run time tabl

         FOR i in 1..10000 LOOP
              execute immediate 'insert into dynamic values('||i||')';--run time insert
         END LOOP;
        OPEN cur_emp FOR 'select * from dynamic';--opening ref cursor
            FETCH cur_emp bulk collect into num_tbl;--bulk inserting in index by table
        close  cur_emp;

        FORALL i in num_tbl.FIRST..num_tbl.LAST --Bulk update
             execute immediate 'insert into dynamic values('||num_tbl(i)||')';
    end;

I am trying to make a run time table named dynamic and inserting data into it from index by table using bulk update,but when i am trying to execute it this error is coming:

ERROR at line 1:
ORA-06550: line 0, column 0:
PLS-00801: internal error [74301

]

declare

     type index_tbl_type IS table of
        number
     index by binary_integer;
     num_tbl index_tbl_type;
     TYPE ref_cur IS REF CURSOR;
     cur_emp ref_cur;
    begin
         execute immediate 'create table dynamic (v_num number)';--Creating a run time tabl

         FOR i in 1..10000 LOOP
              execute immediate 'insert into dynamic values('||i||')';--run time insert
         END LOOP;
        OPEN cur_emp FOR 'select * from dynamic';--opening ref cursor
            FETCH cur_emp bulk collect into num_tbl;--bulk inserting in index by table
        close  cur_emp;

        FORALL i in num_tbl.FIRST..num_tbl.LAST --Bulk update
             execute immediate 'insert into dynamic values('||num_tbl(i)||')';
    end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2024-09-07 09:31:10

FORALL 语句需要 SQL 语句 - INSERT、UPDATE 或 DELETE。 EXECUTE IMMEDIATE 是一个 PL/SQL 语句,这就是您的代码抛出该异常的原因。

在生产中采取这种特技并不是一个好主意。应使用 DDL 脚本而不是动态 SQL 来构建表。

Anyhoo,如果您想在这种动态样式中执行某些操作,请执行以下操作:

步骤 1:创建一个 SQL 类型,可以在 SQL 语句中使用

SQL> create or replace type my_nums as table of number
  2  /

Type created.

SQL>

步骤 2:我的过程版本,它使用 SQL表类型而不是 PL/SQL 类型。我已将 FORALL 子句重写为动态 INSERT 语句,该语句在 TABLE() 子句中使用集合。

SQL> declare
  2
  3        num_tbl my_nums;
  4        TYPE ref_cur IS REF CURSOR;
  5        cur_emp ref_cur;
  6  begin
  7      execute immediate 'create table dynamic (v_num number)';
  8
  9      FOR i in 1..10000 LOOP
 10           execute immediate 'insert into dynamic values('||i||')'
 11      END LOOP;
 12      OPEN cur_emp FOR 'select * from dynamic';
 13      FETCH cur_emp bulk collect into num_tbl;
 14      close  cur_emp;
 15
 16      execute immediate 
 17         'insert into dynamic select * from table(:1)' using num_tbl;
 18  end;
 19  /

PL/SQL procedure successfully completed.

SQL>

第 3 步:有效

SQL> select count(*) from dynamic
  2  /

  COUNT(*)
----------
     20000

SQL>

The FORALL statement is expecting a SQL statement - INSERT, UPDATE or DELETE. EXECUTE IMMEDIATE is a PL/SQL statement, which is why your code is hurling that exception.

Pulling this kind of stunt is not a good idea in Production. Tables should be built using DDL scripts not dynamic SQL.

Anyhoo, if you want to do something in this sort of dynamic stylee this is how to go about it:

Step 1: create a SQL type, which can be used in SQL statements

SQL> create or replace type my_nums as table of number
  2  /

Type created.

SQL>

Step 2: my version of your procedure, which uses the SQL table type instead of the PL/SQL one. I have rewritten the FORALL clause as a dynamic INSERT statement which uses the collection in a TABLE() clause.

SQL> declare
  2
  3        num_tbl my_nums;
  4        TYPE ref_cur IS REF CURSOR;
  5        cur_emp ref_cur;
  6  begin
  7      execute immediate 'create table dynamic (v_num number)';
  8
  9      FOR i in 1..10000 LOOP
 10           execute immediate 'insert into dynamic values('||i||')'
 11      END LOOP;
 12      OPEN cur_emp FOR 'select * from dynamic';
 13      FETCH cur_emp bulk collect into num_tbl;
 14      close  cur_emp;
 15
 16      execute immediate 
 17         'insert into dynamic select * from table(:1)' using num_tbl;
 18  end;
 19  /

PL/SQL procedure successfully completed.

SQL>

Step 3: it works

SQL> select count(*) from dynamic
  2  /

  COUNT(*)
----------
     20000

SQL>
一梦浮鱼 2024-09-07 09:31:10
DECLARE

            TYPE numlist is table of number index by binary_integer;

            var_num numlist;

BEGIN

            for i in 1..1000 loop

                        var_num(i):=i;

            end loop;

            EXECUTE IMMEDIATE 'create table exe_table(col1 number(10))';

            forall i in var_num.first..var_num.last

                        EXECUTE IMMEDIATE 'INSERT INTO exe_table values(:P)' USING var_num(i);

end loop; 

那为什么这有效呢?

DECLARE

            TYPE numlist is table of number index by binary_integer;

            var_num numlist;

BEGIN

            for i in 1..1000 loop

                        var_num(i):=i;

            end loop;

            EXECUTE IMMEDIATE 'create table exe_table(col1 number(10))';

            forall i in var_num.first..var_num.last

                        EXECUTE IMMEDIATE 'INSERT INTO exe_table values(:P)' USING var_num(i);

end loop; 

WHY this is working then?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文