光标名称代替额外变量

时间:2012-10-10 05:06:07

标签: oracle plsql

这是我的程序。其中我使用了名为“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;

1 个答案:

答案 0 :(得分: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;

顺便说一句,我不喜欢

      EXCEPTION
        WHEN OTHERS THEN
          NULL;
      END;

part-如果有异常,你不应该让它沉默......