oracle 语句循环,Oracle中循环语句的几种用法

oracle中的循环语句大致分三种:Loop、While、For

1.Loop

declare i number;

begin

i := 0;

LOOP

Exit When(i > 5);

Dbms_Output.put_line(i);

i := i + 1;

END LOOP;

end;

等价于

declare i number;

begin

i := 0;

loop

if i > 5 then

exit;

end if;

dbms_output.put_line(i);

i := i + 1;

end loop;

end;

执行结果:

0

1

2

3

4

5

2.While

declare i number;

begin

i := 0;

while i < 5 loop

i := i + 1;

dbms_output.put_line(i);

end loop;

end;

执行结果:

1

2

3

4

5

3.For

for循环有两种,普通for循环和游标for循环:

普通for循环实例

declare i number;

begin

i:=0;

for i in 1..5 loop

dbms_output.put_line(i);

end loop;

end;

执行结果:

1

2

3

4

5

游标for循环实例:

declare userRow emp%rowtype;

cursor userRows is select * from emp;

begin

for userRow in userRows

loop

dbms_output.put_line('员工姓名:'|| userRow.ENAME);

end loop;

end;

执行结果:

员工姓名:SMITH

员工姓名:ALLEN

员工姓名:WARD

员工姓名:JONES

员工姓名:MARTIN

员工姓名:BLAKE

员工姓名:CLARK

员工姓名:SCOTT

员工姓名:KING

员工姓名:TURNER

员工姓名:ADAMS

员工姓名:JAMES

员工姓名:FORD

员工姓名:MILLER

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值