0

这是我的程序。我在其中使用了名为“CUR_TO_COLLECT_DATA”的游标。我还使用了一个名为“VAR_CUR_TO_COLLECT_DATA”的变量,它属于“TBL_TRIAL_BALANCE_REPORT”表类型。

现在我不想使用“VAR_CUR_TO_COLLECT_DATA”变量,而是想使用游标的名称,即“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;
4

1 回答 1

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;

部分-如果有异常,您不应该将其静音...

于 2012-10-10T06:37:54.747 回答