如何在存储过程中使用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中如何使用Cookie
Oct 28 面试题
Ibatis中如何提高SQL Map的性能
May 11 面试题
指针和引用有什么区别
Jan 13 面试题
int *p=NULL和*p= NULL有什么区别
Oct 23 面试题
以下的初始化有什么区别
Dec 16 面试题
经典c++面试题五
Dec 17 面试题
什么是索引指示器
Aug 20 面试题
C#中有没有静态构造函数,如果有是做什么用的?
Jun 04 面试题
新锐科技Java程序员面试题
Jul 25 面试题
Overload和Override的区别
Sep 02 面试题
Servlet都有哪些方法?主要作用是什么?
Mar 04 面试题
J2EE中的容器都包括哪些
Aug 21 面试题
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
《猛禽小队》:DC宇宙的又一超级大烂片
2020/04/09 欧美动漫
PHP中static关键字原理的学习研究分析
2011/07/18 PHP
PHP中设置时区方法小结
2012/06/03 PHP
基于PHP生成静态页的实现方法
2013/05/10 PHP
PHP5.3新特性小结
2016/02/14 PHP
php简单截取字符串代码示例
2016/10/19 PHP
js左侧多级菜单动态的解决方案
2010/02/01 Javascript
js实现右下角可关闭最小化div(可用于展示推荐内容)
2013/06/24 Javascript
JS和css实现检测移动设备方向的变化并判断横竖屏幕
2015/05/25 Javascript
JS实现的另类手风琴效果网页内容切换代码
2015/09/08 Javascript
jqGrid用法汇总(全经典)
2016/06/28 Javascript
javascript实现简单的on事件绑定
2016/08/23 Javascript
几行js代码实现自适应
2017/02/24 Javascript
利用JS hash制作单页Web应用的方法详解
2017/10/10 Javascript
jquery自定义显示消息数量
2017/12/19 jQuery
JS控制GIF图片的停止与显示
2019/10/24 Javascript
Node.js控制台彩色输出的方法与原理实例详解
2019/12/01 Javascript
js节流防抖应用场景,以及在vue中节流防抖的具体实现操作
2020/09/21 Javascript
微信小程序实现分页加载效果
2020/11/19 Javascript
用Python将IP地址在整型和字符串之间轻松转换
2017/03/22 Python
酷! 程序员用Python带你玩转冲顶大会
2018/01/17 Python
Django代码性能优化与Pycharm Profile使用详解
2018/08/26 Python
python 解决pycharm运行py文件只有unittest选项的问题
2020/09/01 Python
PyCharm最新激活码(2020/10/27全网最新)
2020/10/27 Python
Debenhams百货英国官方网站:Debenhams UK
2016/07/12 全球购物
Fairyseason:为个人和批发商提供女装和配件
2017/03/01 全球购物
Lookfantastic西班牙官网:英国知名美妆购物网站
2018/06/13 全球购物
读书心得体会
2013/12/28 职场文书
医学生自我评价
2014/01/27 职场文书
股权投资意向书
2014/04/01 职场文书
商场周年庆活动方案
2014/08/19 职场文书
幼儿园小班家长评语
2014/12/30 职场文书
幼儿园六一儿童节主持词
2015/06/30 职场文书
大学文艺委员竞选稿
2015/11/19 职场文书
详解Mysql 函数调用优化
2021/04/07 MySQL
PYTHON 使用 Pandas 删除某列指定值所在的行
2022/04/28 Python