Return 更新请求 PLSQL 的值

Return values of update request PLSQL

这个问题与Get a array of values form a PLSQL Function有关。


我收到了一个非常有效的更新请求。但我想获得 FLUX_ID 的数组,该请求刚刚更新了 LOCKED_FLAG

CREATE TYPE arr_t AS TABLE OF VARCHAR2(1000);
/

CREATE OR REPLACE Function getIDArray
RETURN arr_t is
arr arr_t;

BEGIN

UPDATE DEV_ISB_TRANSACTIONS.BUFFER_FLUX                                    
SET LOCKED_FLAG = 1                                                         
WHERE FLUX_ID IN (
SELECT  FLUX_ID
FROM (
 SELECT FLUX_ID, FLUX, GROUP_STORE_ID, STORE_ID, REFID, FLUX_TYPE, LOCKED_FLAG
 FROM DEV_ISB_TRANSACTIONS.BUFFER_FLUX                                      
 WHERE status = 0                                                           
 AND LOCKED_FLAG = 0                                                                                                             
 ORDER BY DATE_CREATION ASC)
WHERE ROWNUM <= 8)

RETURNING FLUX_ID BULK COLLECT INTO arr;

return arr;
END;
/

编辑 用 Sylvain 的回答更新代码。但是这个请求不return任何东西,也不更新table。 注意:我刚刚重新测试了更新语句,它工作正常(将标志更新为 1)

你知道我应该怎么做吗?

也许是这样的?

CREATE TYPE arr_t AS TABLE OF VARCHAR2(1000);
/

CREATE OR REPLACE Function getIDArray
RETURN arr_t is
  arr arr_t;

BEGIN
  -- all your update statement (I assume this part is OK)
  UPDATE DEV_ISB_TRANSACTIONS.BUFFER_FLUX                                    
  SET LOCKED_FLAG = 1                                                         
  WHERE FLUX_ID IN (
   SELECT  FLUX_ID
   FROM (
     SELECT FLUX_ID, FLUX, GROUP_STORE_ID, STORE_ID, REFID, FLUX_TYPE, LOCKED_FLAG
     FROM DEV_ISB_TRANSACTIONS.BUFFER_FLUX                                      
     WHERE status = 0                                                           
     AND LOCKED_FLAG = 0                                                                                                             
     ORDER BY DATE_CREATION ASC)
  WHERE ROWNUM <= 8)
  -- continuing (no ';' !!!) with the returning clause:    
  RETURNING FLUX_ID BULK COLLECT INTO arr;

  return arr;
END;
/

请注意 RETURNING ...UPDATE 语句的 子句 您不得通过在中间添加分号 ; 来破坏该语句。你会在 select 之后和 where 之前添加这样的 ; 吗?当然没有...

关于使用它:

declare
 result arr_t;
begin
  result := getIDArray();
  for i in 1 .. result.COUNT
  loop
    dbms_output.put_line(result(i));
  end loop;
end;
/

我认为您需要 RETURNING INTO 子句。操作数据的 DML 将 return 作为 OUT 放入变量或集合中。

注意:我没有你的 table 和数据,所以 SCOTT 模式中的 EMP table 示例 -

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    V_NM EMP.ENAME%TYPE;
  3  BEGIN
  4    UPDATE EMP SET EMPNO = 10
  5    WHERE EMPNO = 7369
  6    RETURNING ENAME INTO V_NM;
  7    COMMIT;
  8
  9    DBMS_OUTPUT.put_line('NAME=' || V_NM);
 10  END;
 11  /
NAME=SMITH

PL/SQL procedure successfully completed.

SQL>

EDIT 上面的例子太简单了。在您的情况下,要获取所有 FLUX_ID 的列表,您需要 BULK COLLECTcollection.

考虑这个例子,我在其中获得了所有已更新员工姓名的列表。

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    TYPE t_tab IS TABLE OF emp.ename%TYPE;
  3    l_tab t_tab;
  4  BEGIN
  5    UPDATE emp
  6    SET    sal = 1000
  7    RETURNING ename BULK COLLECT INTO l_tab;
  8
  9    FOR i IN 1..l_tab.count
 10      LOOP
 11         DBMS_OUTPUT.put_line('UPDATED ENAME=' || l_tab(i));
 12    END LOOP;
 13
 14  END;
 15  /
UPDATED ENAME=SMITH
UPDATED ENAME=ALLEN
UPDATED ENAME=WARD
UPDATED ENAME=JONES
UPDATED ENAME=MARTIN
UPDATED ENAME=BLAKE
UPDATED ENAME=CLARK
UPDATED ENAME=SCOTT
UPDATED ENAME=KING
UPDATED ENAME=TURNER
UPDATED ENAME=ADAMS
UPDATED ENAME=JAMES
UPDATED ENAME=FORD
UPDATED ENAME=MILLER

PL/SQL procedure successfully completed.

SQL>