简体   繁体   English

Oracle大容量收集脚本以插入数据

[英]Oracle Bulk Collection Script to Insert Data

I have the following script 我有以下脚本

DECLARE

CURSOR cursor1 IS
    SELECT *
    FROM table1;

TYPE cursor_aat IS TABLE OF cursor1%ROWTYPE;        

l_cursor cursor_aat;

BEGIN        
    OPEN cursor1;

    LOOP

        FETCH cursor1
        BULK COLLECT INTO l_cursor LIMIT 200;

        FOR INDX IN 1 .. l_cursor.COUNT LOOP            
            INSERT INTO new_table
            (col1, col2)
            values
            (l_cursor(INDX).col1, l_cursor(INDX).col2);

        END LOOP;

        EXIT WHEN l_cursor.COUNT < 200;
    END LOOP;

END;

But it complains that it doesn't recognise l_cursor(INDX).col1 . 但它抱怨无法识别l_cursor(INDX).col1 What am I doing wrong here? 我在这里做错了什么?

Why do you use that loop in the first place? 为什么首先使用该循环? It slows things down and creates unnecessary resource consumption on the server. 它会减慢速度,并在服务器上造成不必要的资源消耗。

The whole PL/SQL script can be replaced with a single statement: 整个PL / SQL脚本可以用一个语句替换:

INSERT INTO new_table
(col1, col2)
SELECT col1, col2
FROM table1;

To copy only 200 rows from table1, use the following: 要仅从table1复制200行,请使用以下命令:

INSERT INTO new_table
(col1, col2)
SELECT col1, col2
FROM table1
WHERE rownum < 200;

Note that this does not guarantee which rows are copied as rows can be returned in any order by the SELECT statement. 注意,这不能保证复制哪些行,因为SELECT语句可以按任何顺序返回行。 If you want 200 specific rows you need to apply an order by. 如果要200条特定的行,则需要按以下顺序应用订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM