这几天在写pl/sql,中间用到continue关键字,一不小心就掉进坑里。
oracle版本是11g,贴sql:
造数据:
1 2 3 4 5 6 7 8 9 |
create table address( addr varchar2(10) ); insert into address values ( 'a' ); insert into address values ( 'b' ); insert into address values ( 'c' ); insert into address values ( 'd' ); commit ; |
遍历:
1 2 3 4 5 6 |
declare begin for i in 1..5 loop dbms_output.put_line(i); end loop; end ; |
打印结果:
1
2
3
4
5
1 2 3 4 5 6 |
declare begin for i in ( select * from address) loop dbms_output.put_line(i.addr); end loop; end ; |
打印结果:
a
b
c
d
使用continue:
1 2 3 4 5 6 7 8 9 |
declare begin for i in 1..5 loop if i = 3 then continue ; end if ; dbms_output.put_line(i); end loop; end ; |
打印结果:
1
2
4
5
惯性:
1 2 3 4 5 6 7 8 9 |
declare begin for i in ( select * from address) loop if i.addr = 'c' then continue ; end if; dbms_output.put_line(i.addr); end loop; end ; |
打印结果:
a
b
这里像break。
正确解法:
1 2 3 4 5 6 7 8 9 10 |
declare begin for i in ( select * from address) loop if i.addr = 'c' then goto next_tag; end if; dbms_output.put_line(i.addr); <<next_tag>> null ; end loop; end ; |
打印结果:
a
b
d
打印出continue的效果,收工。