【oracle 流程控制】oracle数据库流程控制语句控制PL/SQL语句

1、条件语句 if else判断

#声明变量
declare employee_sa  number;
begin
select count(*) into employee_sa from employees where salary>6000;
if employee_sa=1 then 
dbms_output.put_line('本公司有一名薪资大于6000的员工');
elsif  employee_sa>1 then 
dbms_output.put_line('本公司有多名薪资大于6000的员工');
esle
dbms_output.put_line('本公司没有薪资大于6000的员工');
end if;
end;

2、case when 分支判断

#声明变量
declare employee_sa  number;
begin
select count(*) into employee_sa from employees where salary>6000;
case when employee_sa=0 then 
dbms_output.put_line('本公司没有薪资大于6000的员工');
when employee_sa=1 then 
dbms_output.put_line('本公司有一名薪资大于6000的员工');
else 
dbms_output.put_line('本公司有多名薪资大于6000的员工');
end case;
end;

3、无条件循环

#无条件循环
-- loop
 -- 循环操作
 -- end loop
#利用无条件循环输出员工employee_id 处于100~106 之间的所有员工姓名
declare e_id number:=100;
declare e_name varchar2(20);
begin
	loop
		if e_id>=106 then
		exit;
		end if;

		e_id:=e_id+1;
		select first_name into e_name from employees where employee_id=e_id;
		dbms_output.put_line(e_id|| '号员工是:'|| e_name);
	end loop;

end;

4、while 循环

#语法
-- while 条件判断 loop
-- 循环操作
-- end loop;

declare e_id number:=100;
declare e_name varchar2(20);
begin
	while e_id<106 loop
	
		e_id:=e_id+1;
		select first_name into e_name from employees where employee_id=e_id;
		dbms_output.put_line(e_id|| '号员工是:'|| e_name);
	end loop;

end;

5、for 循环

#语法(注意:or 循环自行声明变量,因此无需对e_id 声明)
-- for counter
-- in lower_bound upper_bound
-- loop
-- 循环操作
-- end loop;

declare e_name varchar2(20);
begin
	for e_id in 100..106 loop
		select first_name into e_name from employees where employee_id=e_id;
		dbms_output.put_line(e_id|| '号员工是:'|| e_name);
	end loop;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值