oracle选择结构和循环结构

控制结构

在PL/SQL程序中引入了控制结构,包括选择结构,循环结构和跳转结构。

1.选择结构

在PL/SQL程序中,选择结构可以通过IF语句来实现,也可以通过CASE语句来实现。

1.if语句

利用if语句实现选择控制的语法为:

if 条件1 
then 语句1;
elsif 条件2
then 语句2;
…
else 语句n;
end if;

实例1:输入一个员工号,修改该员工的工资。如果该员工为10号部门,则工资增加100元;若为20号部门,则工资增加140元;若为30号部门,则工资增加200元;否则增加300元。

declare
  empno employees.employee_id%type;
  depno departments.department_id%type;
  increment number(4);
begin 
  empno:=&x;
  select department_id into depno from employees
  where employee_id=empno;
  if depno=10 then increment:=100;
  elsif depno=20 then increment:=140;
  elsif depno=30 then increment:=200;
  else increment:=300;
  end if;
  update employees set salary=salary+increment where employee_id=empno;
end;

实例2: 

declare
  score number(3):=85;
  grade varchar2(6);
begin
  if score>=90 then
    grade:='优秀';
  elsif score>=80 then
    grade:='良好';
  elsif score>=70 then
    grade:='中等';
  elsif score>=60 then
    grade:='及格';
  else
    grade:='不及格';
  end if;
  dbms_output.put_line(grade);
end;

实例3:

declare
	--定义四个变量
	v_job test_emp.job%type;
	v_sal number(10);
	v_empno test_emp.empno%type :=&no;
	v_oldsal number(10);
begin
	--查询职位和工资
	select job,sal into v_job,v_oldsal from test_emp where empno=v_empno;
	--如果职位为MANAGER,工资加100,下同
	if 'MANAGER'=v_job then
		v_sal := 100;
	elsif 'SALESMAN'=v_job then
		v_sal := 50;
	elsif 'CLERK'= v_job then
		v_sal := 10;
	else
		v_sal := 0;
	end if;
	--为不同职位的员工加工资
	update test_emp set sal=v_oldsal+v_sal where empno=v_empno;
	commit;
end;

2.case语句 

利用case语句实现选择控制的语法为:

case
	when 条件1 then 语句1;
	when 条件2 then 语句2;
	…
	when 条件n then 语句n;
	else 其他语句;
end case;

 实例1:

declare
  score number(3):=92;
  grade varchar2(6);
begin
  case
  when score>=90 then
    grade:='优秀';
  when score>=80 then
    grade:='良好';
  when score>=70 then
    grade:='中等';
  when score>=60 then
    grade:='及格';
  else
    grade:='不及格';
  end case;
  dbms_output.put_line(grade);
end;

实例2:根据输入的员工号,修改员工的工资。如果该员工工资低于1000元,则工资增加200元;如果工资在1000~2000元之间,则增加140元;如果工资在2000 ~3000元之间,则增加100元;否则增加50元。

declare
  empno employees.employee_id%type;
  sal employees.salary%type;
  increment number(4);
begin
  empno:=&x;
  select salary into sal form employees
  where employee_id=empno;
  case
    when sal<1000 then increment:=200;
    when sal<2000 then increment:=140;
    when sal<3000 then increment:=100
    else increment:=50;
  end case;
  update employees set salary=salary+increment
  where employee_id=empno;
end;

实例3:

declare 
str varchar2(20):='b';
numb number(1);
begin
  numb:=case str
  when 'a' then 1
  when 'b' then 2
  when 'c' then 3
  else  4
end;
dbms_output.put_line(numb);  --输出2
end;

实例4:

declare
	v_job test_emp.job%type;
	v_add number(10);
	v_empno test_emp.empno%type := &no;
	v_oldsal test_emp.sal%type;
begin
	select job,sal into v_job,v_oldsal from test_emp where 
		empno=v_empno;
	case v_job
		when 'MANAGER' then
			v_add := 100;
		when 'SALESMAN' then
			v_add := 50;
		when 'CLERK' then
			v_add := 10;
	else
		v_add :=0;
	end case;
		update test_emp set sal=v_oldsal+v_add where empno=v_empno;
	commit;
end;

2.循环结构: 

在PL/SQL程序中,循环结构分为简单循环,while循环和for循环。

1.简单循环:

PL/SQL程序中简单循环是将循环条件包含在循环体中的循环,语法为:

loop
  循环语句;
  exit when 结束条件;
end loop;

注意:在循环体中一定要包含exit语句,否则程序会进入死循环。


实例1:输出1-10的数字

declare
    v_i number(2) :=1;
begin
  loop
    exit when v_i>10;
    dbms_output.put_line(v_i);
    v_i :=v_i+1;
  end loop;
end;

 实例2:利用简单循环求1-100之间偶数的和。 

declare
  count binary_integer:=1; 
--BINARY_INTENER用来描述不存储在数据库中,但是需要用来计算的带符号的整数值。它以2的补码二进制形式表述。循环计数器经常使用这种类型。
  sum number:=0;
begin
  loop
    if mod(count,2)=0 then
      sum:=sum+count;
    end if
    count:=count+1;
    exit when count>100;
  end loop;
  dbms_output.put_line(sum);
end;

 exit后面是退出循环的条件,一旦count>100,则退出简单循环。
BINARY_INTEGER 表示一个有符号整数,该类型只能在PL/SQL中使用!

2.while循环

利用while语句进行循环时,先判断循环条件只有满足循环条件才能进入循环体内进行循环操作,其语法为:

while 循环条件 loop
    循环语句;
end loop;

实例1:输出1-5的数

declare
    c:=0;
begin
    while c<=5 loop
    c:=c+1;
    dbms_output.put_line(c);
    end loop; 
end;

实例2:1-100的偶数之和

declare 
  count binary_integer:=1;
  sum number:=0;
begin
  while count<=100 loop
    if mod(count,2)=0 then 
      sum:=sum+count;
    end if;
    count:=count+1;
   end loop;
   dbms_output.put_line(sum);
end;

这里与简单循环不同的是,while循环是满足条件才进入循环,而简单循环是一旦满足判断条件则退出循环

3.for循环

在简单循环和while循环中,需要定义循环变量,不断修改循环变量的值,以达到控制循环次数的目的;而在for循环中,不需要定义循环变量,系统会自定义一个循环变量,每次循环时该变量值自动增1或减1,以控制循环次数。for循环的语法为:

for loop_counter in[reverse] low_bound..high_bound loop
  sequence_of_statement;
end loop;

简单说一下:loop_counter为当前循环变量,low_bound为循环变量的下界(最小值),high_boundWie循环变量的上界(最大值)。循环变量不需要显示定义,系统会隐含地将它声明为binary_integer类型,系统默认时,循环变量从下界往上界递增计数,如果使用reverse关键字,则表示循环变量从上界到下界递减计数。


实例1:从 100 开始到 110 结束循环输出以及反转输出

declare
	v_aaa number(10) ;
begin
--从小到大的循环
	for v_aaa in 100 .. 110 loop dbms_output.put_line(v_aaa);
	end loop;
end;

--- 反转输出
declare
	v_aaa number(10) ;
begin

--从大到小的循环•
	for v_aaa in reverse 100..110 loop dbms_output.put_line(v_aaa);
	end loop;
end;

 实例2:1-100的偶数之和

declare
  sum number:=0;
begin
  for count in 1..100 loop
    if mod(count,2)=0 then 
      sum:=sum+count;
    end if;
    count:=count+1;
    end loop;
   dbms_output.put_line(sum);
end;

参考:

 1.Oracle:PL/SQL基础语法详解(选择结构,循环结构以及游标)_散一世繁华,颠半世琉璃的博客-CSDN博客_oracle sql 语法解析

2.PLSQL编程-选择结构_守拙-ATOM的博客-CSDN博客

3.Oracle之选择结构和循环结构_HelloLeif的博客-CSDN博客 

4.PL/SQL选择结构和循环_BIGPLANS的博客-CSDN博客

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的小羽儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值