2012-10-10 94 views
0

這是我的程序。其中我使用了名爲「CUR_TO_COLLECT_DATA」的遊標。 另外我也使用了名爲「VAR_CUR_TO_COLLECT_DATA」的「TBL_TRIAL_BALANCE_REPORT」表格類型的變量。光標名稱代替額外變量

現在我不想使用「VAR_CUR_TO_COLLECT_DATA」變量,並且希望使用遊標的名稱I.e「CUR_TO_COLLECT_DATA」來代替「VAR_CUR_TO_COLLECT_DATA」變量。

正在做這個任務嗎?如果是,那麼請幫助如何?

PROCEDURE PR_TRIAL_BALANCEWITHOUTFOR IS 

    CURSOR CUR_TO_COLLECT_DATA IS(
     SELECT NAME, SUM(CREDIT) AS CREDIT, SUM(DEBIT) AS DEBIT 
     FROM (

       SELECT (SELECT GL_NAME 
         FROM QM_GL 
         WHERE QM_GL.GL_ID = QT_ACCOUNTING.GL_ID) AS NAME, 
         DECODE(QT_ACCOUNTING.TRANS_TYPE, 
          'CR', 
          (QT_ACCOUNTING.TRANS_AMOUNT), 
          0.00) AS CREDIT, 
         DECODE(QT_ACCOUNTING.TRANS_TYPE, 
          'DR', 
          (QT_ACCOUNTING.TRANS_AMOUNT), 
          0.00) AS DEBIT 

       FROM QT_ACCOUNTING, QM_ACCOUNTING_PERIOD 

       WHERE QT_ACCOUNTING.VALUE_DATE BETWEEN 
        QM_ACCOUNTING_PERIOD.PERIODFROM AND 
        QM_ACCOUNTING_PERIOD.PERIODTO 
       AND QM_ACCOUNTING_PERIOD.STATUS = 'O' 

      ) 
     GROUP BY NAME); 

    VAR_CUR_TO_COLLECT_DATA TBL_TRIAL_BALANCE_REPORT%ROWTYPE; 

    BEGIN 

    OPEN CUR_TO_COLLECT_DATA; 

    LOOP 

     FETCH CUR_TO_COLLECT_DATA 
     INTO VAR_CUR_TO_COLLECT_DATA; 

     IF (VAR_CUR_TO_COLLECT_DATA.CREDIT - VAR_CUR_TO_COLLECT_DATA.DEBIT) > 0 THEN 
     INSERT INTO TBL_TRIAL_BALANCE_REPORT 
     VALUES 
      (VAR_CUR_TO_COLLECT_DATA.NAME, 
      (VAR_CUR_TO_COLLECT_DATA.CREDIT - VAR_CUR_TO_COLLECT_DATA.DEBIT), 
      0); 
     END IF; 
     IF (VAR_CUR_TO_COLLECT_DATA.DEBIT - VAR_CUR_TO_COLLECT_DATA.CREDIT) > 0 THEN 
     INSERT INTO TBL_TRIAL_BALANCE_REPORT 
     VALUES 
      (VAR_CUR_TO_COLLECT_DATA.NAME, 
      0, 
      (VAR_CUR_TO_COLLECT_DATA.DEBIT - VAR_CUR_TO_COLLECT_DATA.CREDIT)); 
     END IF; 

     EXIT WHEN CUR_TO_COLLECT_DATA%NOTFOUND; 

    END LOOP; 

    CLOSE CUR_TO_COLLECT_DATA; 

    EXCEPTION 
    WHEN OTHERS THEN 
     DBMS_OUTPUT.PUT_LINE(SQLERRM); 
     BEGIN 
     CLOSE CUR_TO_COLLECT_DATA; 
     EXCEPTION 
     WHEN OTHERS THEN 
      NULL; 
     END; 
    END; 

回答

0

你可以嘗試做這樣的事情:

PROCEDURE PR_TRIAL_BALANCEWITHOUTFOR IS 

    CURSOR CUR_TO_COLLECT_DATA IS(
     SELECT NAME, SUM(CREDIT) AS CREDIT, SUM(DEBIT) AS DEBIT 
     FROM (

       SELECT (SELECT GL_NAME 
         FROM QM_GL 
         WHERE QM_GL.GL_ID = QT_ACCOUNTING.GL_ID) AS NAME, 
         DECODE(QT_ACCOUNTING.TRANS_TYPE, 
          'CR', 
          (QT_ACCOUNTING.TRANS_AMOUNT), 
          0.00) AS CREDIT, 
         DECODE(QT_ACCOUNTING.TRANS_TYPE, 
          'DR', 
          (QT_ACCOUNTING.TRANS_AMOUNT), 
          0.00) AS DEBIT 

       FROM QT_ACCOUNTING, QM_ACCOUNTING_PERIOD 

       WHERE QT_ACCOUNTING.VALUE_DATE BETWEEN 
        QM_ACCOUNTING_PERIOD.PERIODFROM AND 
        QM_ACCOUNTING_PERIOD.PERIODTO 
       AND QM_ACCOUNTING_PERIOD.STATUS = 'O' 

      ) 
     GROUP BY NAME); 

    -- VAR_CUR_TO_COLLECT_DATA TBL_TRIAL_BALANCE_REPORT%ROWTYPE; 

    BEGIN 

    FOR REC_TO_COLLECT_DATA IN CUR_TO_COLLECT_DATA LOOP 

     IF (REC_TO_COLLECT_DATA.CREDIT - REC_TO_COLLECT_DATA.DEBIT) > 0 THEN 
     INSERT INTO TBL_TRIAL_BALANCE_REPORT 
     VALUES 
      (REC_TO_COLLECT_DATA.NAME, 
      (REC_TO_COLLECT_DATA.CREDIT - REC_TO_COLLECT_DATA.DEBIT), 
      0); 
     END IF; 
     IF (REC_TO_COLLECT_DATA.DEBIT - REC_TO_COLLECT_DATA.CREDIT) > 0 THEN 
     INSERT INTO TBL_TRIAL_BALANCE_REPORT 
     VALUES 
      (REC_TO_COLLECT_DATA.NAME, 
      0, 
      (REC_TO_COLLECT_DATA.DEBIT - REC_TO_COLLECT_DATA.CREDIT)); 
     END IF; 

    END LOOP; 

    EXCEPTION 
    WHEN OTHERS THEN 
     DBMS_OUTPUT.PUT_LINE(SQLERRM); 
    END; 

BTW,我不喜歡

 EXCEPTION 
     WHEN OTHERS THEN 
      NULL; 
     END; 

兼職如果你不應該沉默是個例外...

+0

好的,謝謝你的幫助... –