调用一个获取游标的过程

时间:2012-03-28 00:49:00

标签: oracle cursor procedure

不知道是否可能,但必须创建一个过程,在获取查询期间,在另一个过程中浏览数据。

示例:

下面是我的结构是多么真实的另一个例子:

TYPE T_CURSOR IS REF CURSOR;

PROCEDURE PR_ACCOUNT_ACTIVE(CCURSOR OUT T_CURSOR) IS

    CURSOR CURSOR_ACCOUNT IS
        SELECT ID_ACCOUNT, NAME, 0 AS SUM_BALANCE 
          FROM ACCOUNT
         WHERE STATUS = 'A'
         ORDER BY DATE_CREATE DESC;

    REG_ACCOUNT CURSOR_ACCOUNT%ROWTYPE;

    BEGIN

        OPEN CURSOR_ACCOUNT;
        LOOP

            FETCH CURSOR_ACCOUNT INTO REG_ACCOUNT;
            EXIT WHEN CURSOR_ACCOUNT%NOTFOUND;

            /*** 
            At this point I need to call the procedure PR_ACCOUNT_BALANCE (below) and
            her return and use (field SUM_VAL_MONEY) to update the field SUM_BALANCE
            this current cursor (CURSOR_ACCOUNT) and then return to the cursor CCURSOR
            ***/

        END LOOP;
        CLOSE CURSOR_ACCOUNT;

    END;

END PR_ACCOUNT_ACTIVE;


PROCEDURE PR_ACCOUNT_BALANCE(P_ID_ACCOUNT IS NUMBER, CCURSOR OUT T_CURSOR) IS

  BEGIN

    OPEN CCURSOR FOR

      SELECT ID_ACCOUNT
       , SUM(VAL_MONEY) AS SUM_VAL_MONEY
        FROM ACCOUNT_CONTRIBUTION
       WHERE ID_ACCOUNT = P_ID_ACCOUNT
       GROUP BY ID_ACCOUNT

END PR_ACCOUNT_BALANCE;

我的一个大问题是,在两个程序中,返回总是由游标执行,我不能改变它。

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

PROCEDURE PR_ACCOUNT_ACTIVE(CCURSOR OUT T_CURSOR) IS

CURSOR CURSOR_ACCOUNT IS
    SELECT ID_ACCOUNT, NAME, 0 AS SUM_BALANCE 
      FROM ACCOUNT
     WHERE STATUS = 'A'
     ORDER BY DATE_CREATE DESC;

REG_ACCOUNT CURSOR_ACCOUNT%ROWTYPE;

--create a record to fetch the value of the cursor 
TYPE p_rec is RECORD
(
  ID_ACCOUNT ACCOUNT.ID_ACCOUNT%TYPE
 ,SUM_VAL    PLS_INTEGER
);

 v_rec p_rec; --variable of that record

BEGIN

    OPEN CURSOR_ACCOUNT;
    LOOP

        FETCH CURSOR_ACCOUNT INTO REG_ACCOUNT;
        EXIT WHEN CURSOR_ACCOUNT%NOTFOUND;
           PR_ACCOUNT_BALANCE(REG_ACCOUNT.ID_ACCOUNT,v_cursor);
             FETCH  v_cursor INTO v_rec;
               UPDATE ACCOUNT SET sum_balance=v_rec.sum_val 
                    WHERE id_account=v_rec.id_account --assuming id_account is primary key of accounts table  
             CLOSE v_cursor;  

     END LOOP;
     CLOSE CURSOR_ACCOUNT;

END;

END PR_ACCOUNT_ACTIVE;

 PROCEDURE PR_ACCOUNT_BALANCE(P_ID_ACCOUNT IS NUMBER, CCURSOR OUT T_CURSOR) IS

 BEGIN

  OPEN CCURSOR FOR

  SELECT ID_ACCOUNT
   , SUM(VAL_MONEY) AS SUM_VAL_MONEY
    FROM ACCOUNT_CONTRIBUTION
   WHERE ID_ACCOUNT = P_ID_ACCOUNT
   GROUP BY ID_ACCOUNT

END PR_ACCOUNT_BALANCE;
  

我没有编译代码,但我认为这会让你了解这种方法。

<强>建议

  1. 如果您使用的是Oracle11g,请使用automatic subprogramming inline功能优化此过程调用。link
  2. 您可以在游标中使用update clause,在更新时使用where current of