Oracle循环语句及例子(几种循环语句的语法)

xiaoxiao2021-02-27  674

几种循环语句

 

1、loop语句 loop sentence; exit when 条件 end loop; 2、while语句 while 条件 loop sentence; end loop; 3、for语句(变量名为自带计数器) for 变量名 in [reverse] lower_limit..upper_limit loop sentence; end loop;

 

举例:

 

set serveroutput on; --查询employees所有员工的工资总和 declare a1 number; a2 number(8,2); begin --loop循环语句实现 select count(*) into a1 from employees; a2:=0; loop select nvl(salary,0)+a2 into a2 from (select salary,rownum num from employees ) where num=a1; a1:=a1-1; exit when a1<=0; end loop; dbms_output.put_line('loop循环得到所有员工的薪资总和是:'||a2); --while循环语句实现 select count(*) into a1 from employees; a2:=0; while a1>0 loop select nvl(salary,0)+a2 into a2 from (select salary,rownum num from employees ) where num=a1; a1:=a1-1; end loop; dbms_output.put_line('while循环得到所有员工的薪资总和是:'||a2); --loop循环语句实现 select count(*) into a1 from employees; a2:=0; for i in 1..a1 loop select nvl(salary,0)+a2 into a2 from (select salary,rownum num from employees ) where num=i; end loop; dbms_output.put_line('for循环得到所有员工的薪资总和是:'||a2); end;

 

执行结果:

 

匿名块已完成 loop循环得到所有员工的薪资总和是:691416 while循环得到所有员工的薪资总和是:691416 for循环得到所有员工的薪资总和是:691416

 

 

博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-81.html

最新回复(0)