如何更新 oracle 中的 ref cursor 值?

How to update ref cursor values in oracle?

我要取限量号。使用 refcursor 的行数。然后我需要更新同一组记录。可能吗?

create or replace PROCEDURE myproc (
        P_ROWCOUNT    IN    NUMBER,
        OUT_TXN_IDS   OUT   OF_CR_TYPE,
        P_CD_ERROR    OUT   NUMBER,
        P_DS_ERROR    OUT   VARCHAR2
    )
    AS
        V_TXNID NUMBER;

    BEGIN
        P_CD_ERROR := 0;
        P_DS_ERROR  := 'SUCCESS';

        OPEN OUT_TXN_IDS for
            SELECT id FROM table1 WHERE status='N' AND ROWNUM<=P_ROWCOUNT;
        
    EXCEPTION
        WHEN OTHERS THEN
            P_CD_ERROR :=  sqlcode;
            P_DS_ERROR := 'WF-ERROR - myproc - ' || substr(SQLERRM, 1, 200);
            RETURN;
END myproc;

我需要在 refcursor returns 之后将相同的记录更新为状态 Y。我们可以这样做吗?请推荐

我没有您的表格或数据,所以我对其进行了一些简化,但是 - 它仍然可以工作。

初始状态:

SQL> SELECT status, count(*) FROM table1 group by status;

S   COUNT(*)
- ----------
Y          7
N          7

过程:基本上,您将修改由 ref cursor 返回的 ID 表示的行。

SQL> DECLARE
  2     out_txn_ids  SYS_REFCURSOR;
  3     p_rowcount   NUMBER := 5;
  4     l_id         table1.id%TYPE;
  5  BEGIN
  6     OPEN out_txn_ids FOR SELECT id
  7                            FROM table1
  8                           WHERE     status = 'N'
  9                                 AND ROWNUM <= p_rowcount;
 10
 11     LOOP
 12        FETCH out_txn_ids INTO l_id;
 13
 14        EXIT WHEN out_txn_ids%NOTFOUND;
 15
 16        UPDATE table1
 17           SET status = 'Y'
 18         WHERE id = l_id;
 19     END LOOP;
 20
 21     CLOSE out_txn_ids;
 22  END;
 23  /

PL/SQL procedure successfully completed.

结果:

SQL> SELECT status, count(*) FROM table1 group by status;

S   COUNT(*)
- ----------
Y         12
N          2

SQL>