pl/sql之集合-2

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;

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值