如何在存储过程中使用Loop


Posted in 面试题 onJanuary 05, 2016
LOOP statement
The LOOP statement repeats the execution of a statement or a group of statements.

Invocation
This statement can only be embedded in an SQL procedure. It is not an executable statement and cannot be dynamically prepared.

Authorization
No privileges are required to invoke the LOOP statement. However, the authorization ID of the statement must hold the necessary privileges to invoke the SQL statements that are embedded in the LOOP statement.

Syntax
>>-+——–+–LOOP–| SQL-routine-statement |–END LOOP–+——-+-> ‘-label:-’ ‘-label-’

SQL-routine-statement:

.—————————-.
V |
|–+—SQL-procedure-statement–;-+—-+————————|
| .——————————-. |
| V | |
‘—| SQL-function-statement |–;-+-’

Description
label
Specifies the label for the LOOP statement. If the beginning label is specified, that label can be specified on LEAVE and ITERATE statements. If the ending label is specified, a matching beginning label must be specified.
SQL-procedure-statement
Specifies the SQL statements that are to be invoked in the loop. SQL-procedure-statement is only applicable when in the context of an SQL procedure. See SQL-procedure-statement in the description of the Compound SQL (Procedure) statement.
SQL-function-statement
Specifies the SQL statements that are to be invoked in the loop. SQL-function-statement is only applicable when in the context of an SQL function or SQL method. See SQL-function-statement in the description of the FOR statement.
Examples
This procedure uses a LOOP statement to fetch values from the employee table. Each time the loop iterates, the OUT parameter counter is incremented and the value of v_midinit is checked to ensure that the value is not a single space (‘ ‘). If v_midinit is a single space, the LEAVE statement passes the flow of control outside of the loop.

CREATE PROCEDURE LOOP_UNTIL_SPACE(OUT counter INTEGER)
LANGUAGE SQL
BEGIN
DECLARE v_counter INTEGER DEFAULT 0;
DECLARE v_firstnme VARCHAR(12);
DECLARE v_midinit CHAR(1);
DECLARE v_lastname VARCHAR(15);
DECLARE c1 CURSOR FOR
SELECT firstnme, midinit, lastname
FROM employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET counter = -1;
OPEN c1;
fetch_loop:
LOOP
FETCH c1 INTO v_firstnme, v_midinit, v_lastname;
IF v_midinit = ‘ ‘ THEN
LEAVE fetch_loop;
END IF;
SET v_counter = v_counter + 1;
END LOOP fetch_loop;
SET counter = v_counter;
CLOSE c1;
END Related reference
Compound SQL (Procedure) statement

Tags in this post...

面试题 相关文章推荐
PHP如何防止SQL注入
May 03 面试题
名词解释WEB SERVICE,SOAP,UDDI,WSDL,JAXP,JAXM;JSWDL开发包的介绍。
Oct 27 面试题
怎样从/向数据文件读/写结构
Nov 23 面试题
如何打印出当前源文件的文件名以及源文件的当前行号
Apr 05 面试题
请解释流与文件有什么不同
Jul 29 面试题
广州盈通面试题
Dec 05 面试题
简单说下OSPF的操作过程
Aug 13 面试题
什么是组件架构
May 15 面试题
如何利用XMLHTTP检测URL及探测服务器信息
Nov 10 面试题
如何写一个Java类既可以用作applet也可以用作java应用
Jan 18 面试题
Java程序员面试题
Jul 15 面试题
Shell脚本如何向终端输出信息
Apr 25 面试题
SQL Server笔试题
Jan 10 #面试题
Android面试题附答案
Dec 08 #面试题
Android面试题及答案
Sep 04 #面试题
android面试问题与答案
Dec 27 #面试题
Android笔试题总结
Nov 29 #面试题
Android面试宝典
Aug 06 #面试题
Android interview questions
Dec 25 #面试题
You might like
关于页面优化和伪静态
2009/10/11 PHP
PHP JS Ip地址及域名格式检测代码
2013/09/27 PHP
PHP获取mysql数据表的字段名称和详细信息的方法
2014/09/27 PHP
php开发微信支付获取用户地址
2015/10/04 PHP
PHP+Ajax+JS实现多图上传
2016/05/07 PHP
PHP简单实现数字分页功能示例
2016/08/24 PHP
php版微信公众平台之微信网页登陆授权示例
2016/09/23 PHP
浅谈Yii乐观锁的使用及原理
2017/07/25 PHP
在JavaScript中遭遇级联表达式陷阱
2007/03/08 Javascript
js中实现多态采用和继承类似的方法
2014/08/22 Javascript
JavaScript及jquey实现多个数组的合并操作
2014/09/06 Javascript
如何用JavaScript定义一个类
2014/09/12 Javascript
jQuery中prevAll()方法用法实例
2015/01/08 Javascript
深入理解JavaScript系列(39):设计模式之适配器模式详解
2015/03/04 Javascript
js判断请求的url是否可访问,支持跨域判断的实现方法
2016/09/17 Javascript
H5移动端图片压缩上传开发流程
2016/11/09 Javascript
Javascript实现base64的加密解密方法示例
2017/06/27 Javascript
JavaScript学习笔记之函数记忆
2017/09/06 Javascript
javascript用rem来做响应式开发
2018/01/13 Javascript
关于RxJS Subject的学习笔记
2018/12/05 Javascript
vue实现点击按钮下载文件功能
2019/10/11 Javascript
详解python单例模式与metaclass
2016/01/15 Python
Python中用post、get方式提交数据的方法示例
2017/09/22 Python
python画出三角形外接圆和内切圆的方法
2018/01/25 Python
Python创建一个空的dataframe,并循环赋值的方法
2018/11/08 Python
python 多维高斯分布数据生成方式
2019/12/09 Python
pytorch实现mnist数据集的图像可视化及保存
2020/01/14 Python
浅析PyCharm 的初始设置(知道)
2020/10/12 Python
迪卡侬比利时官网:Decathlon比利时
2019/12/28 全球购物
举例说明类变量和实例变量的区别
2016/06/30 面试题
畜牧兽医本科生的自我评价
2014/03/03 职场文书
2014学习全国两会精神心得体会2000字
2014/03/11 职场文书
电信营业员岗位职责
2015/04/14 职场文书
老员工辞职信范文
2015/05/12 职场文书
参观监狱警示教育心得体会
2016/01/15 职场文书
Elasticsearch 批量操作
2022/04/19 Python