如何在存储过程中使用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...

面试题 相关文章推荐
mysql的最长数据库名,表名,字段名可以是多长
Apr 21 面试题
计算 s=(x*y)1/2,用两个宏定义来实现
Aug 11 面试题
Sql面试题
Mar 20 面试题
什么是聚集索引和非聚集索引
Jan 17 面试题
what is the difference between ext2 and ext3
Aug 25 面试题
linux面试题参考答案(10)
Nov 04 面试题
软件测试英文面试题
Oct 14 面试题
GWT的应用有哪两种部署模式
Dec 21 面试题
如何开发安全的AJAX应用
Mar 26 面试题
Java中的异常处理机制的简单原理和应用
Apr 27 面试题
创联软件面试题笔试题
Oct 07 面试题
一套Delphi的笔试题二
May 11 面试题
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
新手学习PHP的一些基础知识分享
2011/07/27 PHP
修改PHP的memory_limit限制的方法分享
2012/02/21 PHP
PHP实例分享判断客户端是否使用代理服务器及其匿名级别
2014/06/04 PHP
php上传中文文件名乱码问题处理方案
2015/02/03 PHP
php实现window平台的checkdnsrr函数
2015/05/27 PHP
详解PHP防止直接访问.php 文件的实现方法
2017/07/28 PHP
PHP文件上传小程序 适合初学者学习!
2019/05/23 PHP
JQuery 动态扩展对象之另类视角
2010/05/25 Javascript
jquery如何实现锚点链接之间的平滑滚动
2013/12/02 Javascript
javascript如何判断输入的url是否正确
2014/04/11 Javascript
JS+CSS实现可拖动的弹出提示框
2015/02/16 Javascript
Google 地图控件集详解及实例代码
2016/08/06 Javascript
[原创]JS基于FileSaver.js插件实现文件保存功能示例
2016/12/08 Javascript
angularJS深拷贝详解
2017/03/23 Javascript
jQuery tip提示插件(实例分享)
2017/04/28 jQuery
基于mpvue搭建微信小程序项目框架的教程详解
2019/04/10 Javascript
vue实现弹幕功能
2019/10/25 Javascript
完美解决通过IP地址访问VUE项目的问题
2020/07/18 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
Python XML RPC服务器端和客户端实例
2014/11/22 Python
使用Python对IP进行转换的一些操作技巧小结
2015/11/09 Python
python opencv实现切变换 不裁减图片
2018/07/26 Python
基于python实现KNN分类算法
2020/04/23 Python
Python常见的pandas用法demo示例
2019/03/16 Python
python实现批量修改服务器密码的方法
2019/08/13 Python
美国新兴城市生活方式零售商:VILLA
2017/12/06 全球购物
.NET面试10题
2014/02/24 面试题
农救科工作职责
2013/11/27 职场文书
黄埔军校观后感
2015/06/10 职场文书
教师理论学习心得体会
2016/01/21 职场文书
《比的意义》教学反思
2016/02/18 职场文书
高三生物教学反思
2016/02/22 职场文书
python tkinter实现定时关机
2021/04/21 Python
详解Vue的options
2021/05/15 Vue.js
教你如何使用Python实现二叉树结构及三种遍历
2021/06/18 Python
Python 发送SMTP邮件的简单教程
2021/06/24 Python