当前位置:网站首页>Oracle 如何使用循环控制关键字exit、goto、continue
Oracle 如何使用循环控制关键字exit、goto、continue
2022-04-22 10:13:00 【=PNZ=BeijingL】

目录
1. exit 使用
exit用于跳出循环, 在10g和11g都可以正常使用
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 if (i = 5) then
4 exit;
5 end if;
6 dbms_output.put_line(i);
7 end loop;
8 end;
9 /
PL/SQL procedure successfully completed
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 if (i = 5) then
4 exit;
5 end if;
6 dbms_output.put_line(i);
7 end loop;
8 end;
9 /
PL/SQL procedure successfully completed
2. goto使用
goto可以在10g和11g使用,有的人说11g不能使用经过验证此说法是错误的, goto使用方法如下
- <<next_step>> 是循环标签,名称可以自定义,比如示例中也可以替换为<<next>>
- 当使用goto next_step的时候,程序会跳转到定义的 <<next_step>>标签
- <<next_step>> 后面不能跟end loop 或者exception关键字,所以使用NULL隔开
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> begin
2
3 for i in 1 .. 20 loop
4 if (i > 5) then
5 goto next_step;
6 end if;
7 dbms_output.put_line(i);
8
9 <<next_step>> --goto跳转到这里进入下次循环
10 NULL;
11 end loop;
12
13 end;
14 /
PL/SQL procedure successfully completed
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 if (i > 5) then
4 goto next_step;
5 end if;
6 dbms_output.put_line(i);
7
8 <<next_step>> --goto跳转到这里进入下次循环
9 NULL;
10 end loop;
11 end;
12 /
PL/SQL procedure successfully completed
3. continue使用
continue用于继续下次循环, continue关键字是11g增加,因此在Oracle 10g中使用的时候会提示 没有声明CONTINUE
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 -- 判断
4 if (i = 5) then
5 continue;
6 end if;
7 dbms_output.put_line(i);
8 end loop;
9 end;
10 /
ORA-06550: line 6, column 7:
PLS-00201: identifier 'CONTINUE' must be declared
ORA-06550: line 6, column 7:
PL/SQL: Statement ignored
continue关键字在Oracle 11g中可以使用
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 -- 判断
4 if (i = 5) then
5 continue;
6 end if;
7 dbms_output.put_line(i);
8 end loop;
9 end;
10 /
PL/SQL procedure successfully completed
4. oracle不支持break
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 -- 判断
4 if (i = 5) then
5 break;
6 end if;
7 dbms_output.put_line(i);
8 end loop;
9 end;
10 /
ORA-06550: line 6, column 7:
PLS-00201: identifier 'BREAK' must be declared
ORA-06550: line 6, column 7:
PL/SQL: Statement ignored
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> begin
2 for i in 1 .. 20 loop
3 -- 判断
4 if (i = 5) then
5 break;
6 end if;
7 dbms_output.put_line(i);
8 end loop;
9 end;
10 /
ORA-06550: 第 6 行, 第 7 列:
PLS-00201: identifier 'BREAK' must be declared
ORA-06550: 第 6 行, 第 7 列:
PL/SQL: Statement ignored
版权声明
本文为[=PNZ=BeijingL]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Beijing_L/article/details/124300563
边栏推荐
- 分表和分区的区别
- Drop down refresh and pull-up load of product list of uni app project
- IOS development - Database - Introduction to basic knowledge (01)
- Difference between sub table and partition
- LC301. Remove invalid parentheses
- You need to specify one of MySQL_ ROOT_ PASSWORD, MYSQL_ ALLOW_ EMPTY_ PASSWORD and MYSQ
- Review of QT layout management
- The web application scans the code to obtain the user information of zhezheng nail
- Generalized Robust Regression for Jointly Sparse Subspace Learning
- 最通俗易懂的依赖注入之生命周期
猜你喜欢

一文学会text-justify,orientation,combine文本属性

Beyond iterm! Known as the next generation terminal artifact, I can't put it down after using it!

西门子PLC能否通过以太网数据模块实现无线通讯?

最通俗易懂的依赖注入之生命周期

stringstream的用法

Simple operation of QT axobject Library

Dext diagnostic database supporting AUTOSAR classic and adaptive platform

自学编程千万别再傻傻地读理论书,程序员:这都是别人玩剩下的

二极管工作原理

三分钟快速了解互动涂鸦
随机推荐
SQL 表达式
二极管工作原理
J'ai eu un entretien d'une demi - heure avec Semaphore.
Glide设置圆角图片(支持自定义圆角位置)
Nacos
[SQL Server] SQL overview
最通俗易懂的依赖注入之生命周期
中介者模式
DA14580BLE点亮LED
Secondary encapsulation of requests for uni app projects
golang 时间格式化
柯里化的两种写法(弄懂`fn.length`与`fn.toString()`)
Android被爆安全漏洞 根源是苹果的无损音频编解码器
linux7静默安装oracle11g
QT event filter instance
UVC camera 封装类
ORACLE数据库查询锁表语句sql脚本,以及删除锁信息脚本(数据库开发ETL、DBA必备)
[moistening C language] Beginner - start from scratch - 5 (modular design - function, value transmission and address transmission)
01背包问题——以及变形问题
Transform based deep learning target detection (Detr model)