0

我正在阅读有关 BULK COLLECT 和 dbms_sql 用法的更多信息,并尝试将其应用于我的一个过程在我的存储过程中,核心逻辑是将另一个表的值插入到表中

CREATE OR replace PROCEDURE My_procedure (pi_date       IN DATE,
                                          po_error      OUT VARCHAR2,
                                          po_error_desc OUT nocopy VARCHAR2)
AS
  curr_date DATE;
BEGIN
    CURR_DATE := PI_DATE;

    INSERT INTO t1
                (col1,
                 col2,
                 col3,
                 col4,
                 col5)
    SELECT t2.col1,
           t2.col2,
           t2.col3,
           t2.col4,
           CURR_DATE
EXCEPTION
  WHEN OTHERS THEN
             PO_ERROR := -1;

             PO_ERROR_DESC := 'proc nam : '
                              || 'my_procedure'
                              || ', err_num :'
                              || SQLCODE
                              || ' | , err_msg :'
                              || SQLERRM;

             ROLLBACK;

             DBMS_SESSION.free_unused_user_memory;
END;

但是,由于要插入另一个表的数据很大,所以在我的第二个修改过程中

我使用了 BULK COLLECT 和 dbms_sql 如下

CREATE OR replace PROCEDURE My_procedure (pi_date       DATE,
                                          po_error      OUT VARCHAR2,
                                          po_error_desc OUT nocopy VARCHAR2)
AS
  v_curr_date DATE;
  l_col1                       dbms_sql.Varchar2_Table;
  l_col2                       dbms_sql.Varchar2_Table;
  l_col3                       dbms_sql.Number_Table;
  l_col4                       dbms_sql.Number_Table;

  CURSOR c1 IS
    SELECT *
    FROM   t2;

BEGIN
    V_CURR_DATE := PI_DATE;

    PO_ERROR := 0;

    OPEN c1;

    LOOP
        FETCH c1 bulk collect INTO l_col1, l_col2, l_col3, l_col4 limit 1000;

        forall indx IN 1..l_col1.COUNT
          INSERT INTO t2
                      (col1,
                       col2,
                       col3,
                       col4,
                       col5)
          VALUES      (L_col1(indx),
                       L_col2(indx),
                       L_col3(indx),
                       L_col4(indx),
                       V_CURR_DATE);

EXCEPTION
  WHEN OTHERS THEN
             PO_ERROR := -1;

             PO_ERROR_DESC := 'proc nam : '
                              || 'my_procedure'
                              || ', err_num :'
                              || SQLCODE
                              || ' | , err_msg :'
                              || SQLERRM;

             ROLLBACK;

             DBMS_SESSION.free_unused_user_memory;
END; 

所以这是我的第二个例子,但是我按照文档使用了 BULK collect

有人可以指出确切的用法dbms_sql.Varchar2_Table吗?

如上所示,如果 col1 的实际长度是 VARCHAR2(40) 但dbms_sql.Varchar2_Table使用VARCHAR2(2000)

TYPE varchar2_table IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
4

1 回答 1

0

当您使用LIMIT子句时,您的批量收集循环需要是一个完整的循环:

-- Open the cursor
OPEN cursor_name;
-- Loop through cursor records
LOOP

   -- Fetch the cursor results into a collection limited to a set figure
   FETCH cursor_name BULK COLLECT INTO table
   LIMIT 1000;

   -- For every record in the collection
   FORALL x IN INDICES OF table
      INSERT INTO target_table
      (col)
      VALUES
      table(x);

   -- Set up the limit loop exit criteria
   EXIT WHEN table.COUNT < 1000; -- Less that the limit you set    
-- End the loop
END LOOP;
-- Close the cursor
CLOSE cursor_name;

这个伪代码片段应该让您开始正确的道路,让您的批量代码工作。您当前的DBMS_SQL表格声明看起来不错。

希望能帮助到你...

于 2012-04-23T12:21:35.637 回答