oracle – 如何将数据插入PL / SQL表类型而不是PL / SQL表?

我有一个TDATAMAP表,它有大约1000万条记录,我想将所有记录读取到PL / SQL表类型变量中,与一些条件匹配,最后在登台表中插入所有必需的记录.请告诉我是否可以使用PL / SQL表typle变量和BULK INSERT / COLLECT来完成它.我也担心代码的性能.
最佳答案
你可以,但你可能不应该一次将1000万条记录加载到内存中 – 只要有足够的内存来容纳那么多内存.通常,BULK COLLECT与LIMIT子句一起使用,一次处理有限数量的行,例如1000.

documentation

The BULK COLLECT clause lets you fetch entire columns from the result
set, or the entire result set at once. The following example,
retrieves columns from a cursor into a collection:

DECLARE
   TYPE NameList IS TABLE OF emp.ename%TYPE;
   names NameList;
   CURSOR c1 IS SELECT ename FROM emp WHERE job = 'CLERK';
BEGIN
   OPEN c1;
   FETCH c1 BULK COLLECT INTO names;
   ...
   CLOSE c1;
END;

The following example uses the LIMIT clause. With each iteration of
the loop, the FETCH statement fetches 100 rows (or less) into index-by
table acct_ids. The previous values are overwritten.

DECLARE
   TYPE NumList IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
   CURSOR c1 IS SELECT acct_id FROM accounts;
   acct_ids NumList;
   rows NATURAL := 100;  -- set limit
BEGIN
   OPEN c1;
   LOOP
      /* The following statement fetches 100 rows (or less). */
      FETCH c1 BULK COLLECT INTO acct_ids LIMIT rows;
      EXIT WHEN c1%NOTFOUND;
      ...
   END LOOP;
   CLOSE c1;
END;

转载注明原文:oracle – 如何将数据插入PL / SQL表类型而不是PL / SQL表? - 代码日志