在FOR循环ORACLE中声明游标

时间:2014-02-04 10:13:48

标签: sql oracle oracle11g cursor oracle-sqldeveloper

我正在尝试执行以下过程。我所拥有的是,在wo_list的最开始有一个sql,我需要迭代它并将此wo_list中的pyid值传递给另一个我需要创建另一个游标的查询。所以我打开一个for循环,启动我的wo_list游标循环,现在在这个循环中我需要通过传递wo_list中的一个值来从另一个查询创建另一个游标。

      CREATE OR REPLACE
     PROCEDURE FIX_DIMS_DOC_PROD_DATA_1
    AS

  tempivaultprod VARCHAR(8);
  tempivaultdoc  VARCHAR(8);

 CURSOR wo_list
 IS
 SELECT A.Pyid,
  A.Pxcreatedatetime,
  A.Pxcreateoperator,
  A.Pxcreateopname,
  A.Packageid,
  E.Doc_Desc,
  A.Concat_Prod_Desc,
  A.Primary_Ecid,
  A.Primary_Cust_Name,
  A.Isnamdocument,
  A.PYSTATUSWORK,
  F.Req_No,
  F.Req_Src,
  A.concat_prod_id,
  A.documenttypeid
FROM DIMS_DOC_MASTER A,
  Dims_Doc_Map_Product B,
  Dims_Doc_Type E ,
  Dims_Doc_Asctd_Req_Dtl F
WHERE B.Dims_Doc_Id = A.PYID
AND NOT EXISTS
  (SELECT 1
  FROM Dims_Prod_Type_Doc_Type C
  WHERE C.DIMS_PROD_ID = B.Dimsdocprodid
  AND C.Doc_Type_Id    = A.Documenttypeid
  )
 AND E.Doc_Id = A.Documenttypeid
 AND A.PYID   = F.DIMS_DOC_ID (+);
 BEGIN
 FOR DIMS_DOC_REC IN wo_list
 LOOP
  BEGIN
  CURSOR wo_list1
  IS
    SELECT DIMSDOCPRODID
    FROM DIMS_DOC_MAP_PRODUCT
    WHERE DM.DIMS_DOC_ID=DIMS_DOC_REC.pyid;

  FOR DIMS_DOC_PROD IN wo_list1
  LOOP
  BEGIN
    dbms_output.enable(30000);
    SELECT ivault_doc_type
    INTO tempivaultdoc
    FROM dims_doc_prod_details
    WHERE doc_type_id=DIMS_DOC_REC.documenttypeid;
    SELECT ivault_prod_type
    INTO tempivaultprod
    FROM dims_doc_prod_details
    WHERE actual_dims_product=DIMS_DOC_PROD.DIMSDOCPRODID;
    INSERT
    INTO Dims_Prod_Type_Doc_Type
      (
        DOC_TYPE_ID,
        DIMS_PROD_ID,
        CBOVERRIDE,
        REQUIRED,
        PODDOCUMENT,
        WHEM_OR_EMEA_REGION,
        APACREGION,
        ISACTIVE,
        CRE_TS,
        UPDT_TS,
        CRE_USR_SID,
        UPDT_USR_SID,
        WCBI_UPDATETIMESTAMP,
        IVAULT_PROD_TYPE_ID,
        IVAULT_DOC_TYPE_ID,
        ROUTINGRULE_DESC,
        ROUTINGRULE_CD,
        OLD_IVAULT_PROD_ID,
        OLD_IVAULT_DOC_ID,
        EMEA_ROUTING_CD,
        EMEA_ROUTING_DESC,
        APAC_ROUTING_DESC,
        APAC_ROUTING_CD,
        NASQUEUENAME,
        IS_ORIG_REQD,
        LATAM_ROUTING_DESC,
        LATAM_ROUTING_CD
      )
      VALUES
      (
        DIMS_DOC_REC.documenttypeid,
        DIMS_DOC_PROD.DIMSDOCPRODID,
        'N',
        'N',
        'N',
        'N',
        'N',
        'N',
        systimestamp,
        systimestamp,
        'DIMS',
        'DIMS',
        SYSTIMESTAMP,
        tempivaultprod,
        tempivaultdoc,
        'Document Specialist',
        'DocumentSpecialist',
        NULL,
        NULL,
        'DocControlReview',
        'Doc Control Review Only',
        'In-Country Review',
        'InCountryReview',
        NULL,
        NULL,
        'In-Country Review',
        'InCountryReview'
      );
     COMMIT;
    END;
 END LOOP;
END;
 END LOOP;
EXCEPTION
 WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE
(
  'Error:'||SQLERRM||CHR(10)||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
)
 ;
END;

2 个答案:

答案 0 :(得分:5)

您必须将光标定义放在程序的“开始”之前。

您可以声明它以便它需要一个变量。

 cursor wo_list1(p_doc_id number) is
            select dimsdocprodid
              from dims_doc_map_product
             where dm.dims_doc_id = p_doc_id;

然后在程序中使用它你想要的值

   for dims_doc_prod in wo_list1(dims_doc_rec.pyid) loop

答案 1 :(得分:0)

编译错误是您问题的解决方案。但为什么我们需要这么多游标。我们可以在单个查询中执行此操作,并且它比当前方法更好。下面的查询您可以使用它。我已经采取了一些假设,例如dims_doc_prod_details表总是有两列(doc_type_id,actual_dims_product)值。如果不是,我们可以根据需要更改查询。我没有从select中过滤所需的列。

INSERT INTO Dims_Prod_Type_Doc_Type
SELECT M.*,
  N.DIMSDOCPRODID,
  N.ivault_doc_type,
  N.tempivaultprod
FROM
  (SELECT A.Pyid,
    A.Pxcreatedatetime,
    A.Pxcreateoperator,
    A.Pxcreateopname,
    A.Packageid,
    E.Doc_Desc,
    A.Concat_Prod_Desc,
    A.Primary_Ecid,
    A.Primary_Cust_Name,
    A.Isnamdocument,
    A.PYSTATUSWORK,
    F.Req_No,
    F.Req_Src,
    A.concat_prod_id,
    A.documenttypeid
  FROM DIMS_DOC_MASTER A,
    Dims_Doc_Map_Product B,
    Dims_Doc_Type E ,
    Dims_Doc_Asctd_Req_Dtl F
  WHERE B.Dims_Doc_Id = A.PYID
  AND NOT EXISTS
    (SELECT 1
    FROM Dims_Prod_Type_Doc_Type C
    WHERE C.DIMS_PROD_ID = B.Dimsdocprodid
    AND C.Doc_Type_Id    = A.Documenttypeid
    )
  AND E.Doc_Id = A.Documenttypeid
  AND A.PYID   = F.DIMS_DOC_ID (+)
  ) M,
  DIMS_DOC_MAP_PRODUCT N,
  dims_doc_prod_details O
WHERE M.pyid        =N.doc_type_id
AND M.documenttypeid=o.doc_type_id
AND N.DIMSDOCPRODID =o.actual_dims_product;