知识大全 OracleDML子句RETURNINGINTO
Posted 文字
篇首语:读书志在圣贤,为官心存君国。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 OracleDML子句RETURNINGINTO相关的知识,希望对你有一定的参考价值。
OracleDML子句RETURNINGINTO 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
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 t ;
DROP SEQUENCE t _seq;
CREATE TABLE t (
id NUMBER( )
description VARCHAR ( )
CONSTRAINT t _pk PRIMARY KEY (id)
);
CREATE SEQUENCE t _seq;
INSERT INTO t VALUES (t _seq nextval ONE );
INSERT INTO t VALUES (t _seq nextval O );
INSERT INTO t VALUES (t _seq nextval THREE );
MIT;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 t id%TYPE;
BEGIN
INSERT INTO t VALUES (t _seq nextval FOUR )
RETURNING id INTO l_id;
MIT;
DBMS_OUTPUT put_line( ID= || l_id);
END;
/
ID=
PL/SQL procedure successfully pleted
SQL>The syntax is also available for update and delete statements
SET SERVEROUTPUT ON
DECLARE
l_id t id%TYPE;
BEGIN
UPDATE t
SET description = description
WHERE description = FOUR
RETURNING id INTO l_id;
DBMS_OUTPUT put_line( UPDATE ID= || l_id);
DELETE FROM t
WHERE description = FOUR
RETURNING id INTO l_id;
DBMS_OUTPUT put_line( DELETE ID= || l_id);
MIT;
END;
/
UPDATE ID=
DELETE ID=
PL/SQL procedure successfully pleted
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 t id%TYPE;
l_tab t_tab;
BEGIN
UPDATE t
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;
MIT;
END;
UPDATE ID=
UPDATE ID=
UPDATE ID=
PL/SQL procedure successfully pleted
SQL>We can also use the RETURNING INTO clause in bination with bulk binds
SET SERVEROUTPUT ON
DECLARE
TYPE t_desc_tab IS TABLE OF t description%TYPE;
TYPE t_tab IS TABLE OF t %ROWTYPE;
l_desc_tab t_desc_tab := t_desc_tab( FIVE SIX SEVEN );
l_tab t_tab;
BEGIN
FORALL i IN l_desc_tab first l_desc_tab last
INSERT INTO t VALUES (t _seq nextval l_desc_tab(i))
RETURNING id description BULK COLLECT INTO l_tab;
FOR i IN l_tab first l_tab last LOOP
DBMS_OUTPUT put_line( INSERT ID= || l_tab(i) id ||
DESC= || l_tab(i) description);
END LOOP;
MIT;
END;
/
INSERT ID= DESC=FIVE
INSERT ID= DESC=SIX
INSERT ID= DESC=SEVEN
PL/SQL procedure successfully pleted
SQL>This functionality is also available from dymanic SQL
SET SERVEROUTPUT ON
DECLARE
TYPE t_tab IS TABLE OF t id%TYPE;
l_tab t_tab;
BEGIN
EXECUTE IMMEDIATE UPDATE t
SET description = description
RETURNING id INTO :l_tab
RETURNING 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;
MIT;
END;
/
UPDATE ID=
UPDATE ID=
UPDATE ID=
PL/SQL procedure successfully pleted
cha138/Article/program/Oracle/201311/18329相关参考
SQL必知必会:WHERE子句操作符[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!——此文章
知识大全 Oracle10g新特性—增强的CONNECTBY子句
Oracle10g新特性—增强的CONNECTBY子句 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
知识大全 SQL Server 2005数据库中的Output子句
SQLServer2005数据库中的Output子句 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
知识大全 oracle中Where子句顺序是否对SQL性能有影响
oracle中Where子句顺序是否对SQL性能有影响 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
系统环境 操作系统Windows 数据库 OracleiR()forNT企业版 安装路径C:\\ORACLE 解释 可用于表空间回滚段表索引分区快照快照日志 参数名
在应用系统开发初期由于开发数据库数据比较少对于查询SQL语句复杂视图的编写刚开始不会体会出SQL语句各种写法的性能优劣但是如果将应用系统提交实际应用后随着数据库中数据的增加系统的响应速度就成为目前
()选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名FROM子句中写在最后的表(基础表drivingtable)将被最先处理在F
()选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名FROM子句中写在最后的表(基础表drivingtable)将被最先处理在
从Oraclei开始索引跳跃式扫描特性可以允许优化器使用组合索引即便索引的前导列没有出现在WHERE子句中索引跳跃式扫描比全索引扫描要快的多下面的程序清单显示出性能的差别 createindex
Oracle常用SQL技巧 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! SELECT子句中避免