在游标中使用Rownum批量收集Oracle

时间:2015-06-01 14:58:21

标签: oracle cursor rownum forall bulk-collect

我正在尝试使用rownum来模拟自动编号的列,因为我需要将其用作ID。由于它是一个ID,如果没有MAX(ID)记录,我会查看最终表。 我遇到的问题是当我想在游标中或你调用时或者想要使用函数时进行算术运算。当我想用DBMS_OUTPUT打印时,ROWNUM(v_id)字段为空。任何人都知道如何在不使用序列的情况下解决它? 这里放了示例代码。

declare

  max_id number;
  CURSOR INSRT(w_max number) IS
  SELECT f_max_fact_sap(to_number(V_ID),w_max) AS V_ID,Seriei,serief
  FROM (SELECT To_Char(ROWNUM) AS V_ID, A.*
          FROM (SELECT DISTINCT a.matnr, a.seriei, a.serief,a.xblnr,a.fecha_sap, ((SERIEF-SERIEI)+1) AS rango
                  FROM SOPFUN.TT_ZMOVIMI_FACTURADAS a
                WHERE 0 =(SELECT COUNT(1)
                            FROM PA_ZMOVIMI_FACTURADAS B
                            WHERE A.SERIEI = B.SERIEI
                              AND A.SERIEF = B.SERIEF
                              AND A.MATNR = B.MATNR
                              AND A.FECHA_SAP=B.FECHA_SAP)
                    AND A.FECHA_SAP IS NOT NULL) A);
  TYPE T_INSRT IS TABLE OF INSRT%ROWTYPE INDEX BY PLS_INTEGER;
  V_INSRT T_INSRT;


begin
  SELECT Max(Nvl(ID,10000)) INTO MAX_ID-- To Proof because the table is empty
    FROM PA_ZMOVIMI_FACTURADAS;

OPEN INSRT(MAX_ID);
    LOOP
    FETCH INSRT BULK COLLECT INTO V_INSRT LIMIT 1000;
        FOR I IN 1 .. V_INSRT.Count loop
          DBMS_OUTPUT.PUT_LINE('ID: ' ||V_INSRT(I).V_ID||' SI: '||V_INSRT(I).SERIEI||' SI: '||V_INSRT(I).SERIEF||' OPERACION: '||to_char(f_max_fact_sap(V_INSRT(I).V_ID,MAX_ID)));
        end loop;        
    EXIT WHEN INSRT%NOTFOUND;
   END LOOP;
end;

0 个答案:

没有答案