使用提交间隔从一个表插入另一个表

时间:2017-03-29 08:08:38

标签: sql stored-procedures plsql oracle-sqldeveloper plsqldeveloper

我编写了一个将数据从一个表插入另一个表的过程。有大量数据 - 总共有1.06亿行。所以我想在查询上放置一个提交间隔。

我发现这个脚本通常会起作用,不需要时间来完成工作,但我不知道为什么会出现错误

INSERT INTO dlc_pr_activity_remise   
VALUES (add all values using ga_array(i).<value in select>)

错误:

  

错误(51,1):PL / SQL:忽略SQL语句
  错误(51,47):PL / SQL:ORA-00917:缺少逗号

我的程序:

create or replace
PROCEDURE ACTIVITY1 AS 
BEGIN
DECLARE 
commit_interval pls_integer := 250000 ; 
uncommitted pls_integer := 0; 
fetch_size  pls_integer := 2500 ; 
cursor g1 is SELECT prr.EVT_DECLENCHEUR ,
                    prr.BQ_CDF ,
                    prr.BQ_DOM ,
                    prr.NUM_COMMERCANT ,
                    prr.TYPE_CONTRAT_COM ,
                    prr.COMMERCANT_SIRET ,
                    prr.TYPE_PRE_COMP ,
                    prr.BQ_CDF_ID_EBF ,
                    prr.BQ_DOM_ID_EBF ,
                    prr.DAT_TRAITEMENT ,
                    prr.EVT_TYPE_OPERATION ,
                    prr.DEV_MT_CRE ,
                    prr.NBR_DECI_MT_CRE ,
                    prr.REF_FICHIER_TRAITE ,
                    prr.NUM_CTC ,
                    prr.REF_FICHIER_ORIGINE ,
                    prr.ORIGINE_FLUX ,
                    prr.NUM_EST_REMISE ,
                    prr.NUM_REMISE_ORIGINE ,
                    prr.NUM_MACHINE ,
                    prr.REF_ARCHIVAGE_REM ,
                    prr.DAT_REMISE ,
                    prr.SUPPORT_REMISE ,
                    prr.DAT_REMISE_CALCULEE ,
                    prr.DAT_VALEUR_REMISE ,
                    prr.COD_APPLI ,
                    to_date(SYSDATE,'DD/MM/YYYY')
                    FROM dlc_pr_remise prr
                    WHERE NOT EXISTS
                      (SELECT *
                      FROM DLC_PR_ACTIVITY_REMISE prao
                      WHERE prao.num_est_remise = prr.num_est_remise
                      );
TYPE GL_T is table of gl%rowtype ; 
gl_array GL_T; 
begin 
open gl ; 
loop 
fetch gl 
bulk collect 
into gl_array 
limit fetch_size ; 
forall i in 1 .. gl_arary.count 
INSERT INTO dlc_pr_activity_remise values(add all values using ga_array(i).<value in select>)
uncommitted :+ uncommitted + sql%rowcount ; 
exit when gl_arary.count < fetch_size ; 
if uncommitted >= commit_interval 
then 
commit ; 
uncommitted := 0; 
end if ; 
end loop ; 
commit ; 
close gl; 
END ;
END ACTIVITY1;

3 个答案:

答案 0 :(得分:1)

你可以用它。代码中的错误很少,这就是编译时出错的原因。请将您的代码与下面的代码进行比较,找出您犯错误的地方。我的评论是内联的。

CREATE OR REPLACE PROCEDURE ACTIVITY1
AS  

   fetch_size  pls_integer := 100 ;

   CURSOR gl
   IS
      SELECT prr.EVT_DECLENCHEUR,
             prr.BQ_CDF,
             prr.BQ_DOM,
             prr.NUM_COMMERCANT,
             prr.TYPE_CONTRAT_COM,
             prr.COMMERCANT_SIRET,
             prr.TYPE_PRE_COMP,
             prr.BQ_CDF_ID_EBF,
             prr.BQ_DOM_ID_EBF,
             prr.DAT_TRAITEMENT,
             prr.EVT_TYPE_OPERATION,
             prr.DEV_MT_CRE,
             prr.NBR_DECI_MT_CRE,
             prr.REF_FICHIER_TRAITE,
             prr.NUM_CTC,
             prr.REF_FICHIER_ORIGINE,
             prr.ORIGINE_FLUX,
             prr.NUM_EST_REMISE,
             prr.NUM_REMISE_ORIGINE,
             prr.NUM_MACHINE,
             prr.REF_ARCHIVAGE_REM,
             prr.DAT_REMISE,
             prr.SUPPORT_REMISE,
             prr.DAT_REMISE_CALCULEE,
             prr.DAT_VALEUR_REMISE,
             prr.COD_APPLI,
             TO_DATE (SYSDATE, 'DD/MM/YYYY')
        FROM dlc_pr_remise prr
       WHERE NOT EXISTS
                (SELECT *
                   FROM DLC_PR_ACTIVITY_REMISE prao
                  WHERE prao.num_est_remise = prr.num_est_remise);

   TYPE GL_T IS TABLE OF gl%ROWTYPE;

   gl_array         GL_T;
BEGIN
   OPEN gl;

   LOOP
      FETCH gl BULK COLLECT INTO gl_array LIMIT fetch_size;

      FORALL i IN 1 .. gl_arary.COUNT
         INSERT INTO dlc_pr_activity_remise -- I assume the dlc_pr_activity_remise table has same columns which you are selectig in cursor.
              VALUES gl_arary (i);

      --Exit the loop when you cursor is empty.
      EXIT WHEN gl%NOTFOUND;

   END LOOP;

   COMMIT;

  CLOSE gl;

END ACTIVITY1;

答案 1 :(得分:0)

cursor g1 is select acct_nbr from dwc_tmp_acct_rcvbl;

上面的光标选择必须替换为:

SELECT prr.EVT_DECLENCHEUR ,
prr.BQ_CDF ,
prr.BQ_DOM ,
prr.NUM_COMMERCANT ,
prr.TYPE_CONTRAT_COM ,
prr.COMMERCANT_SIRET ,
prr.TYPE_PRE_COMP ,
prr.BQ_CDF_ID_EBF ,
prr.BQ_DOM_ID_EBF ,
prr.DAT_TRAITEMENT ,
prr.EVT_TYPE_OPERATION ,
prr.DEV_MT_CRE ,
prr.NBR_DECI_MT_CRE ,
prr.REF_FICHIER_TRAITE ,
prr.NUM_CTC ,
prr.REF_FICHIER_ORIGINE ,
prr.ORIGINE_FLUX ,
prr.NUM_EST_REMISE ,
prr.NUM_REMISE_ORIGINE ,
prr.NUM_MACHINE ,
prr.REF_ARCHIVAGE_REM ,
prr.DAT_REMISE ,
prr.SUPPORT_REMISE ,
prr.DAT_REMISE_CALCULEE ,
prr.DAT_VALEUR_REMISE ,
prr.COD_APPLI ,
to_date(SYSDATE,'DD/MM/YYYY')
  FROM dlc_pr_remise prr
  WHERE NOT EXISTS
    (SELECT *
    FROM DLC_PR_ACTIVITY_REMISE prao
    WHERE prao.num_est_remise = prr.num_est_remise
    )

然后,这一行:

insert into dwc_tmp_acct_rcvbl_year_col (acct_nbr) 

必须替换为

INSERT INTO dlc_pr_activity_remise values(add all values using ga_array(i).<value in select>)

希望这有帮助。

答案 2 :(得分:0)

未提交的增量需要     未提交:=未提交+ sql%rowcount;

不像邮寄     未提交:+未提交+ sql%rowcount;