Oracle RETURNING INTO Ó÷¨Ê¾Àý
The RETURNING INTO clause allows us to return column values for rows affected by DML statements. The following test table is used to demonstrate this clause.
DROP TABLE t1;
DROP SEQUENCE t1_seq;
CREATE TABLE t1 (
id NUMBER(10),
description VARCHAR2(50),
CONSTRAINT t1_pk PRIMARY KEY (id)
);
CREATE SEQUENCE t1_seq;
INSERT INTO t1 VALUES (t1_seq.nextval, 'ONE');
INSERT INTO t1 VALUES (t1_seq.nextval, 'TWO');
INSERT INTO t1 VALUES (t1_seq.nextval, 'THREE');
COMMIT;
When we insert data using a sequence to generate our primary key value, we can return the primary key value as follows.
SET SERVEROUTPUT ON
DECLARE
l_id t1.id%TYPE;
BEGIN
INSERT INTO t1 VALUES (t1_seq.nextval, 'FOUR')
RETURNING id INTO l_id;
COMMIT;
DBMS_OUTPUT.put_line('ID=' || l_id);
END;
/
ID=4
PL/SQL procedure successfully completed.
SQL>
The syntax is also available for update and delete statements.
SET SERVEROUTPUT ON
DECLARE
l_id t1.id%TYPE;
BEGIN
UPDATE t1
SET description = description
WHERE description = 'FOUR'
RETURNING id INTO l_id;
DBMS_OUTPUT.put_line('UPDATE ID=' || l_id);
DELETE from t1
WHERE description = 'FOUR'
RETURNING id INTO l_id;
DBMS_OUTPUT.put_line('DELETE ID=' || l_id);
COMMIT;
END;
/
UPDATE ID=4
DELETE ID=4
PL/SQL procedure successfully completed.
SQL>
When DML affects multiple rows we can still use the RETURNING INTO, but now we must return the values into a collection using the BULK COLLECT clause.
SET SERVEROUTPUT ON
DECLARE
TYPE t_tab IS TABLE OF t1.id%TYPE;
l_tab t_tab;
BEGIN
UPDATE t1
SET description = description
RETURNING id BULK COLLECT INTO l_tab;
FOR i IN l_tab.first .. l_tab.last LOOP
DBMS_OUTPUT.put_line('UPDATE ID=' || l_tab(i));
END LOOP;
COMMIT;
END;
/
UPDATE ID=1
UPDATE ID=2
UPDATE ID=3
PL/SQL procedure successfully completed.
SQL>
We can also use the RETURNING INTO clause in combination with bulk binds.
SET SERVEROUTPUT ON
Ïà¹ØÎĵµ£º
Ò» ÒýÑÔ
Êý¾Ý²Ö¿â¼¼ÊõÊÇÄ¿Ç°ÒÑÖªµÄ±È½Ï³ÉÊìºÍ±»¹ã·º²ÉÓõĽâ¾ö·½°¸£¬ÓÃÓÚÕûºÍµçÐÅÔËÓªÆóÒµÄÚ²¿ËùÓзÖÉ¢µÄÔʼҵÎñÊý¾Ý£¬²¢Í¨¹ý±ã½ÝÓÐЧµÄÊý¾Ý·ÃÎÊÊֶΣ¬¿ÉÒÔÖ§³ÖÆóÒµÄÚ²¿²»Í¬²¿ÃÅ£¬²»Í¬ÐèÇ󣬲»Í¬²ã´ÎµÄÓû§Ëæʱ»ñµÃ×Ô¼ºËùÐèµÄÐÅÏ¢¡£Êý¾Ý²Ö¿âϵͳÐèÒªÄܹ»¼°Ê±µØ×·×ٺͷÖÎö´óÁ¿µÄÀúÊ·Êý¾Ý£¬²¢Äܹ»¼°Ê±×ö³ö·ÖÎöºÍÔ¤²â£¬Òò´ËÊ ......
Navicat for xxx ÊÇÒ»¸öÓÅÐãµÄÊý¾Ý¿â¹ÜÀí¿Í»§¶Ë£¬ÓÐ MySQL¡¢Oracle µÈ°æ±¾¡£½¨Òé´ó¼Ò×îºÃÓà Enterprise °æ±¾£¬¹¦ÄÜÈ«ÃæһЩ£¬µ«½ÏÖ®ÓÚÃâ·ÑµÄ Lite °æ£¬ÆóÒµ°æ¿ÉÊÇÒª»¨Òø×ÓÂòµÄ¡£
°²×° Navicat for Oracle ºó£¬Ê×ÏÈÐèÒª½¨Ò»¸ö“Á¬½Ó”£¬×÷Ϊij¸ö¹ÜÀíÈÎÎñµÄ±êʶ£¬ÒòΪ×÷Ϊ¿ ......
Ëæ×ÅÈ«ÇòÐÅÏ¢»¯Ê±´úµÄµ½À´£¬ÐÅÏ¢Êý¾ÝÔ½À´Ô½³ÉΪÆóÒµ¹Ø×¢µÄ½¹µã£¬¼´ÔÚ¸÷ÖÖ×ÔÈ»ÔÖº¦»òÕßÈËΪÆÆ»µµÄÇé¿öÏ£¬ÈçºÎ±£Ö¤Éú²úÊý¾ÝµÄ°²È«ºÍ¹Ø¼üÒµÎñµÄ²»ÖжÏÔËÐС£
´«Í³µÄ±£»¤·½·¨ÀëÏß±¸·Ý¡¢±¸·Ý½éÖÊÒìµØ±£´æÔÚÒ»¶¨³Ì¶ÈÉÏ¿ÉÒÔ½â¾öÉÏÊöÎÊÌ⣬µ«ÎªÁËÄܹ»¶ÔÒµÎñϵͳÌṩ¸ü¸ßµÄʵʱÐԺͿɿ¿ÐÔ£¬±£Ö¤¹Ø¼üÒµÎñ7x24²» ......
ÔÎÄ´«ËÍÃÅ£ºhttp://www.blogjava.net/improviser/archive/2008/12/07/244911.html
ÔÚǰһƪÎÄÕÂÖмòµ¥½éÉÜÁËJAVA³ÌÐòÈçºÎµ÷ÓÃOracle´æ´¢¹ý³ÌµÄ»ù±¾Óï·¨³ÌÐò½á¹¹£¬ ±¾ÎĽ«½éÉÜJAVA¸úOracleµÄÁíÒ»ÖÖ¹Øϵ£¬¼´Í¨¹ýJAVA±àдOracle´æ´¢¹ý³Ì¡£ ͨ³£Çé¿öÏ£¬ÎÒÃǶ¼ÊÇʹÓÃOracleÊý¾Ý¿âϵͳÖеÄps/sqlÓï¾äÀ´Îªoracle±àд ......