当前位置:网站首页>pl/sql之集合-2
pl/sql之集合-2
2022-07-26 08:38:00 【赟文武】
bulk collect
弥补了select…into、cursor一次只能接收一行数据
select…into…与bulk collect
一、关键部分语法
select 列名 bulk collect into 集合变量名 from 表名;
二、简单示例
--输出emp表中所有人姓名
declare
--声明嵌套表类型a
type a is table of emp.ename%type;
--声明嵌套表类型a的承载变量b
b a;
begin
--将emp表中所有ename赋值给b嵌套表变量
select ename bulk collect into b from emp;
--循环输出嵌套表b中内容
for i in b.first..b.last loop
dbms_output.put_line(b(i));
end loop;
end;
--2
declare
--声明嵌套表类型a
type a is table of emp%rowtype;
--声明嵌套表类型a的承载变量b
b a;
begin
--将emp表中所有ename赋值给b嵌套表变量
select * bulk collect into b from emp;
--循环输出嵌套表b中内容
for i in b.first..b.last loop
dbms_output.put_line(b(i).ENAME);
end loop;
end;
cursor与bulk collect
一、关键部分语法
fetch 游标名 bulk collect into 集合变量名;
二、简单示例
declare
--声明一个名为a的游标
cursor a is select ename from emp;
--声明一个嵌套表类型
type b is table of emp.ename%type;
--声明一个承载嵌套表集合变量
c b;
begin
--打开游标
open a;
--匹配数据
fetch a BULK collect into c;
--关闭游标
close a;
--循环输出c中元素
for i in c.first..c.last loop
dbms_output.put_line(c(i));
end loop;
end;
--2
declare
--声明一个名为a的游标
cursor a is select * from emp;
--声明一个嵌套表类型
type b is table of emp%rowtype;
--声明一个承载嵌套表集合变量
c b;
begin
--打开游标
open a;
--匹配数据
fetch a BULK collect into c;
--关闭游标
close a;
--循环输出c中元素
for i in c.first..c.last loop
dbms_output.put_line(c(i).ename);
end loop;
end;
批量绑定forall
一、关键部分语法
forall 变量 in 集合变量名;
二、简单示例
--根据emp表中部门编号删除dept中信息
--隐式游标
begin
delete from dept where deptno in(select distinct deptno from emp);
end;
--批量绑定
declare
--声明嵌套表类型
type a is table of dept.deptno%type;
--声明承载嵌套表类型变量
b a;
--声明变量
c dept.deptno%type;
begin
--赋值
SELECT DISTINCT DEPTNO BULK collect INTO b FROM EMP;
--批量绑定
forall i in b.first..b.last
delete from DEPT where deptno=b(i);
end;
边栏推荐
- QT note 1
- Winter vacation homework & Stamp cutting
- 2022-7-8 personal qualifying 5 competition experience (supplementary)
- The effective condition of MySQL joint index and the invalid condition of index
- SSH,NFS,FTP
- [recommended collection] summary of MySQL 30000 word essence - partitions, tables, databases and master-slave replication (V)
- JS工具函数大全
- Oracle 19C OCP 1z0-082 certification examination question bank (19-23)
- Maximum common substring & regularity problem
- Problems caused by slivereappbar
猜你喜欢
随机推荐
Super nice navigation page (static page)
Install HR schema, example, and Scott schema on Oracle and MySQL
Dear teachers, how can sqlserver get DDL in flinkcdc?
P1825 [USACO11OPEN]Corn Maze S
Flutter upgrade 2.10
Kotlin data type
B title: razlika priority queue approach
基于C语言实现的人机交互软件
Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
Oracle 19C OCP certification examination software list
Kotlin operator
Solve the problem of C # calling form controls across threads
Oracle 19C OCP 1z0-083 question bank (1-6)
Why reserve a capacitor station on the clock output?
Xshell batch send command to multiple sessions
【FreeSwitch开发实践】使用SIP客户端Yate连接FreeSwitch进行VoIP通话
QSS add resource file of QT
22-07-16 personal training match 3 competition experience
In the first year of L2, the upgrade of arbitrum nitro brought a more compatible and efficient development experience
How to safely delete a useless activity in Android studio