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

面试题 相关文章推荐
联强国际笔试题面试题
Jul 10 面试题
一个SQL面试题
Aug 21 面试题
数据库的约束含义
Sep 09 面试题
如何在.net Winform里面显示PDF文档
Sep 11 面试题
端口镜像是怎么实现的
Mar 25 面试题
说出你对remoting 和webservice的理解和应用
Jun 08 面试题
linux面试题参考答案(10)
Oct 26 面试题
软件缺陷的分类都有哪些
Aug 22 面试题
恒华伟业笔试面试题
Feb 26 面试题
当文件系统受到破坏时,如何检查和修复系统?
Mar 09 面试题
Java Servlet的主要功能和作用是什么
Feb 14 面试题
J2EE的优越性主要表现在哪些方面
Mar 28 面试题
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借助phpmailer发送邮件
2015/05/11 PHP
php实现面包屑导航例子分享
2015/12/19 PHP
php处理抢购类功能的高并发请求
2018/02/08 PHP
JavaScript 的方法重载效果
2009/08/07 Javascript
jquery.fileEveryWhere.js 一个跨浏览器的file显示插件
2011/10/24 Javascript
javascript动态加载实现方法一
2012/08/22 Javascript
js获取控件位置以及不同浏览器中的差别介绍
2013/08/08 Javascript
js获取或设置当前窗口url参数的小例子
2013/10/14 Javascript
jquery实现当滑动到一定位置时固定效果
2014/06/17 Javascript
浅谈jquery的html方法里包含特殊字符的处理
2016/11/30 Javascript
node.js中http模块和url模块的简单介绍
2017/10/06 Javascript
详解NodeJs开发微信公众号
2018/05/25 NodeJs
vue项目首屏打开速度慢的解决方法
2019/03/31 Javascript
vuex state中的数组变化监听实例
2019/11/06 Javascript
js实现文章目录索引导航(table of content)
2020/05/10 Javascript
[04:19]DOTA2亚洲邀请赛 现场花絮
2015/03/11 DOTA
[02:19]DOTA选手解说齐贺岁
2018/02/11 DOTA
常见的python正则用法实例讲解
2016/06/21 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
Python基于sklearn库的分类算法简单应用示例
2018/07/09 Python
python下载微信公众号相关文章
2019/02/26 Python
Python叠加两幅栅格图像的实现方法
2019/07/05 Python
python使用装饰器作日志处理的方法
2019/07/11 Python
Python 使用threading+Queue实现线程池示例
2019/12/21 Python
python 消费 kafka 数据教程
2019/12/21 Python
Python连接Oracle之环境配置、实例代码及报错解决方法详解
2020/02/11 Python
Python 读取WAV音频文件 画频谱的实例
2020/03/14 Python
英国著名书店:Foyles
2018/12/01 全球购物
Strathberry苏贝瑞中国官网:西班牙高级工匠手工打造
2020/10/19 全球购物
商务日语毕业生自荐信范文
2013/11/14 职场文书
远程教育心得体会
2014/01/03 职场文书
质量提升方案
2014/06/16 职场文书
国际贸易毕业生求职信
2014/07/20 职场文书
Python基础 括号()[]{}的详解
2021/11/07 Python
mysql分组后合并显示一个字段的多条数据方式
2022/01/22 MySQL
Java实现添加条码或二维码到Word文档
2022/06/01 Java/Android