返回更新请求PLSQL的值

时间:2015-01-08 09:06:50

标签: sql oracle 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的答案更新代码。但是这个请求不会返回任何内容,也不会更新表。 注意:我刚刚重新测试了更新语句,它工作正常(将标志更新为1)

你是否知道我应该怎么做?

2 个答案:

答案 0 :(得分:2)

这样的事可能吗?

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;
/

答案 1 :(得分:0)

我认为你需要RETURNING INTO条款。操作数据的DML将作为OUT返回到变量或集合中。

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

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>

编辑以上示例过于简单。在您的情况下,要获取所有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>