oracle动态 returning,ORACLE RETURNING 用法总结
ORACLE RETURNING用法总结
场景
在存储过程、PL/SQL块里需要返回INSERT、DELETE、UPDATE、MERGE等DML语句执行后的信息时使用,合理使用returning能够简化程序逻辑、提高程序性能。
概述
创建测试表
create table hh_emp_test as select * from scott.emp;
使用returning语句
declare
v_empno hh_emp_test.empno%type;
v_ename hh_emp_test.ename%type;
begin
update hh_emp_test set ename=‘test‘ where empno=7369 returning empno,ename into v_empno,v_ename;
rollback;
dbms_output.put_line(v_empno||‘-‘||v_ename);
end;
输出
7369-test
场景分类
dml修改单行数据
使用方法见概述,此部分较简单,略。
dml修改多行数据
使用TABLE类型
举例:
declare
type v_tp_tab_empno is table of hh_emp_test.empno%type index by pls_integer;
v_tab_empno v_tp_tab_empno;
type v_tp_tab_ename is table of hh_emp_test.ename%type index by pls_integer;
v_tab_ename v_tp_tab_ename;
begin
update hh_emp_test set ename=‘test‘ where deptno=10 returning empno,ename bulk collect into v_tab_empno,v_tab_ename;
rollback;
for i in 1..v_tab_empno.count loop
dbms_output.put_line(v_tab_empno(i)||‘-‘||v_tab_ename(i));
end loop;
end;
输出:
7782-test
7839-test
7934-test
注意:多行returning须用bulk
collect into
使用RECORD类型
示例:
declare
type v_tp_rec is record(empno number,ename varchar2(50));
type v_tp_tab is table of v_tp_rec index by pls_integer;
v_tab v_tp_tab;
begin
update hh_emp_test set ename=‘test‘ where deptno=10 returning empno,ename bulk collect into v_tab;
rollback;
for i in 1..v_tab.count loop
dbms_output.put_line(v_tab(i).empno||‘-‘||v_tab(i).ename);
end loop;
end;
输出:
7782-test
7839-test
7934-test
?
Dml修改单行+动态sql
示例:
declare
v_empno hh_emp_test.empno%type;
v_ename hh_emp_test.ename%type;
begin
execute immediate ‘update hh_emp_test set ename=‘‘test‘‘ where empno=:empno returning empno,ename into :v_empno,:v_ename‘
using 7369
returning into v_empno, v_ename;
rollback;
dbms_output.put_line(v_empno || ‘-‘ || v_ename);
end;
输出:
7369-test
注意:returning
into在动态sql内部和外面都要写,且外面的returning后面不加字段直接into。
using在returning前面
into后面变量名不固定,注意冒号(:),可以是命名规则下的任意字符。
dml修改多行+动态sql
使用TABLE类型
示例:
declare
type v_tp_tab_empno is table of hh_emp_test.empno%type index by pls_integer;
v_tab_empno v_tp_tab_empno;
type v_tp_tab_ename is table of hh_emp_test.ename%type index by pls_integer;
v_tab_ename v_tp_tab_ename;
begin
execute immediate ‘update hh_emp_test set ename=‘‘test‘‘ where deptno=:deptno returning empno,ename into :v_tab_empno,:v_tab_ename‘
using 10
returning bulk collect
into v_tab_empno, v_tab_ename;
rollback;
for i in 1 .. v_tab_empno.count loop
dbms_output.put_line(v_tab_empno(i) || ‘-‘ || v_tab_ename(i));
end loop;
end;
输出:
7782-test
7839-test
7934-test
注意:动态sql内部仍然是returning into而不是returning bulk collect into
returning bulk collect into要写在外面,且后面同样不能是record
使用RECORD类型
示例:
declare
type v_tp_rec is record(
empno number,
ename varchar2(50));
type v_tp_tab is table of v_tp_rec index by pls_integer;
v_tab v_tp_tab;
begin
execute immediate ‘update hh_emp_test set ename=‘‘test‘‘ where deptno=10 returning empno,ename :v_tab‘
returning bulk collect
into v_tab;
rollback;
for i in 1 .. v_tab.count loop
dbms_output.put_line(v_tab(i).empno || ‘-‘ || v_tab(i).ename);
end loop;
end;
执行报错:
ORA-06550: 第 9 行, 第 5 列:
PLS-00429: RETURNING 子句不支持的功能
ORA-06550: 第 8 行, 第 3 列:
PL/SQL: Statement ignored
可见动态sql执行时,多行returning的多个字段须定义多个table类型的变量,目前为止(包括12c)不支持reurning record类型的语法。
forall中的returning
使用RECORD类型
示例:
declare
type v_tp_rec is record(
empno number,
ename varchar2(50));
type v_tp_tab is table of v_tp_rec index by pls_integer;
v_tab v_tp_tab;
type t_tp_rec_source is table of hh_emp_test%rowtype index by pls_integer;
t_tab_source t_tp_rec_source;
cursor v_cur is
select * from hh_emp_test;
begin
open v_cur;
fetch v_cur bulk collect
into t_tab_source limit 3;
while t_tab_source.count > 0 loop
forall i in 1 .. t_tab_source.count
update hh_emp_test
set ename = ‘test‘
where empno = t_tab_source(i).empno
returning empno, ename bulk collect into v_tab;
rollback;
for i in 1 .. v_tab.count loop
dbms_output.put_line(v_tab(i).empno || ‘-‘ || v_tab(i).ename);
end loop;
fetch v_cur bulk collect
into t_tab_source limit 3;
end loop;
close v_cur;
end;
输出:
7369-test
7499-test
7521-test
7566-test
7654-test
7698-test
7782-test
7839-test
7844-test
7900-test
7902-test
7934-test
使用TABLE类型
示例:
declare
type v_tp_tab_empno is table of hh_emp_test.empno%type index by pls_integer;
v_tab_empno v_tp_tab_empno;
type v_tp_tab_ename is table of hh_emp_test.ename%type index by pls_integer;
v_tab_ename v_tp_tab_ename;
type t_tp_rec_source is table of hh_emp_test%rowtype index by pls_integer;
t_tab_source t_tp_rec_source;
cursor v_cur is
select * from hh_emp_test;
begin
open v_cur;
fetch v_cur bulk collect
into t_tab_source limit 3;
while t_tab_source.count > 0 loop
forall i in 1 .. t_tab_source.count
update hh_emp_test
set ename = ‘test‘
where empno = t_tab_source(i).empno
returning empno, ename bulk collect into v_tab_empno,v_tab_ename;
rollback;
for i in 1 .. v_tab_empno.count loop
dbms_output.put_line(v_tab_empno(i) || ‘-‘ || v_tab_ename(i));
end loop;
fetch v_cur bulk collect
into t_tab_source limit 3;
end loop;
close v_cur;
end;
输出:
7369-test
7499-test
7521-test
7566-test
7654-test
7698-test
7782-test
7839-test
7844-test
7900-test
7902-test
7934-test
小结:
Forall的使用和静态sql dml修改多行的方法类似。
总结
Oracle Returning语句随场景不同,语法有变化,要注意动态sql returning多行的情况不能使用record只能使用table类型。
原文:http://www.cnblogs.com/mellowsmile/p/5748925.html
oracle动态 returning,ORACLE RETURNING 用法总结相关推荐
- oracle 动态条件查询语句,教您Oracle动态查询语句的用法
Oracle动态查询语句是一类特殊的查询语句,下面就为您详细介绍Oracle动态查询语句的语法,如果您对Oracle动态查询方面感兴趣的话,不妨一看. 1. 当使用EXECUTE IMMEDIATE语 ...
- Oracle 动态游标 PL/SQL 动态SQL语句 open for [using] 语句
PL/SQL:open for [using] 语句 2017年07月19日 09:52:55 学孩儿无牙哭做粥 阅读数:681 标签: oracleSQLPLSQL 更多 个人分类: ORACLES ...
- Oracle 动态SQL语句
Oracle 动态SQL语句 EXECUTE IMMEDIATE代替了以前Oracle8i中DBMS_SQLpackage包. 它解析并马上执行动态的SQL语句或非运行时创建的PL/SQL块 ...
- bulk怎么使用oracle,oracle学习之bulk collect用法
通过bulk collect减少loop处理的开销,使用Bulk Collect提高Oracle查询效率 Oracle8i中首次引入了Bulk Collect特性,该特性可以让我们在PL/SQL中能使 ...
- Oracle 存储过程,Hibernate 调用存储过程,JDBC调用存储过程,Oracle 动态SQL
Oracle 存储过程学习 目录 Oracle 存储过程........................................................................ ...
- Oracle中游标Cursor基本用法详解
这篇文章主要介绍了Oracle中游标Cursor基本用法详解,还是比较全面的,具有一定参考价值,需要的朋友可以了解下. 查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT ...
- Oracle数据库中SQL语句用法(一)
Copyright © 2019 @Linyer. All Rights Reserved 下接Oracle数据库中SQL语句用法(二)[点击以查看] 目录 第1章:编写基本的SQL SELECT语句 ...
- bde oracle 商友的流程_bde oracle 商友的流程_BDE动态连接Oracle数据库
BDE动态连接Oracle数据库 说明:系统是三层架构:数据库是Oracle9, Server采用BDE连接数据库,使用scktsrvr.exe,Client采用SocketConnection连接到 ...
- oracle动态注册和静态注册监听器
oracle动态注册和静态注册的区别:动态监听不需要在listener.ora文件中记录关于数据的任何信息,只需要将监听器的配置信息写入到该文件.例如: LISTENER = (DESCRIPTIO ...
- ORACLE 动态SQL中的多个单引号
今天在项目中遇到动态拼sql的语句, 语句如下: v_sql := 'update Table_Test t ' ||' set t.field1 = ''' || 变量1 || ''',' -- ...
最新文章
- Linux下查看Nginx,tomcat等的并发连接数和连接状态
- 隔离见证地址区别_科普:比特币钱包的隔离见证地址与普通地址有何区别?
- 跳水比赛背后的隐形教练现身了!百度智能云还完成了历史性的大升级
- JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
- C/Cpp / typeof、_typeof 和 _typeof_ 区别和联系
- 第七次scrum meeting记录
- 海淀某互联网公司鼓励员工尽量住在公司
- ES6中关于set数据结构详解
- imovie打开视频卡死解决办法
- Python scipy拟合分布
- Mysql简介和Mysql优化查询的方法
- 爬虫 - scrapy框架设置代理
- 7-22 切分表达式——写个tokenizer吧 (20 分)
- matlab按图像边缘抠图_干货:PS抠图的九种方法,最后一个简直是万能
- RS485MODBUS RTU转PROFINET网关/PROFINET转MODBUS RTU网关将施耐德ATV610变频器接入西门子1500 PROFINET网络配置方法
- 2020华为软挑热身赛代码开源-思路大起底(华为软件精英挑战赛编程闯关)
- python爬取pubmed的文献_使用python來調用pubmed API快速整理文獻
- NDK开发——Android Studio+CMake实现QQ变声效果
- kubelet 压力驱逐 - The node had condition:[DiskPressure]
- ORACLE 错误 1659,数据库导入dmp临时表空间内存不够
热门文章
- 由于找不到openni2_Python OpenNI2 libOpenNI2.so问题
- 唯品会测试工程师实习生招聘2014春
- confluence 制作流程图_要什么第三方流程图制作软件,微软的Visio 2016不香吗?
- 深度学习 - 胶囊网络
- oracle_利用ctl文件实现批量导入
- 首选项配置+Eslint+prettier+Vetur
- Matlab——二进制转十进制(包含小数转换)
- 无效的目标发行版: 11
- TCGA的gdc-client的下载优化
- 我在成都火车站捡了个彝族美女 第64节:聊天记录中的真相