The RETURNING INTO clause allows us to return column values for rows affected by DML statements
DROP TABLE t
DROP SEQUENCE t
CREATE TABLE t
id NUMBER(
description VARCHAR
CONSTRAINT t
);
CREATE SEQUENCE t
INSERT INTO t
INSERT INTO t
INSERT INTO t
COMMIT;When we insert data using a sequence to generate our primary key value
SET SERVEROUTPUT ON
DECLARE
l_id t
BEGIN
INSERT INTO t
RETURNING id INTO l_id;
COMMIT;
DBMS_OUTPUT
END;
/
ID=
PL/SQL procedure successfully completed
SQL>The syntax is also available for update and delete statements
SET SERVEROUTPUT ON
DECLARE
l_id t
BEGIN
UPDATE t
SET description = description
WHERE description =
RETURNING id INTO l_id;
DBMS_OUTPUT
DELETE FROM t
WHERE description =
RETURNING id INTO l_id;
DBMS_OUTPUT
COMMIT;
END;
/
UPDATE ID=
DELETE ID=
PL/SQL procedure successfully completed
SQL>When DML affects multiple rows we can still use the RETURNING INTO
SET SERVEROUTPUT ON
DECLARE
TYPE t_tab IS TABLE OF t
l_tab t_tab;
BEGIN
UPDATE t
SET description = description
RETURNING id BULK COLLECT INTO l_tab;
FOR i IN l_tab
DBMS_OUTPUT
END LOOP;
COMMIT;
END;
UPDATE ID=
UPDATE ID=
UPDATE ID=
PL/SQL procedure successfully completed
SQL>We can also use the RETURNING INTO clause in combination with bulk binds
SET SERVEROUTPUT ON
DECLARE
TYPE t_desc_tab IS TABLE OF t
TYPE t_tab IS TABLE OF t
l_desc_tab t_desc_tab := t_desc_tab(
l_tab t_tab;
BEGIN
FORALL i IN l_desc_tab
INSERT INTO t
RETURNING id
FOR i IN l_tab
DBMS_OUTPUT
END LOOP;
COMMIT;
END;
/
INSERT ID=
INSERT ID=
INSERT ID=
PL/SQL procedure successfully completed
SQL>This functionality is also available from dymanic SQL
SET SERVEROUTPUT ON
DECLARE
TYPE t_tab IS TABLE OF t
l_tab t_tab;
BEGIN
EXECUTE IMMEDIATE
SET description = description
RETURNING id INTO :l_tab
RETURNING BULK COLLECT INTO l_tab;
FOR i IN l_tab
DBMS_OUTPUT
END LOOP;
COMMIT;
END;
/
UPDATE ID=
UPDATE ID=
UPDATE ID=
PL/SQL procedure successfully completed
SQL>
From:http://tw.wingwit.com/Article/program/Oracle/201311/18329.html