如何在存储过程中使用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面试题及答案-编程题
Oct 14 面试题
什么是serialVersionUID
Mar 04 面试题
写出二分查找算法的两种实现
May 13 面试题
请写出 BOOL flag 与"零值"比较的 if 语句
Feb 29 面试题
什么是SQL Server的确定性函数和不确定性函数
Aug 04 面试题
XML文档面试题
Aug 05 面试题
了解AppleTalk协议吗
Apr 01 面试题
什么是符号链接,什么是硬链接?符号链接与硬链接的区别是什么?
May 03 面试题
什么时候用assert
May 08 面试题
送给程序员的20个Java集合面试问题
Aug 06 面试题
创联软件面试题笔试题
Oct 07 面试题
什么是ESB?请介绍一下ESB?
May 27 面试题
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中var_export与var_dump的区别分析
2010/08/21 PHP
apache mysql php 源码编译使用方法
2012/05/03 PHP
PHP统计当前在线用户数实例讲解
2015/10/21 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
2015/11/05 PHP
Symfony2安装的方法(2种方法)
2016/02/04 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
2016/06/30 PHP
Laravel 集成微信用户登录和绑定的实现
2019/12/27 PHP
php数组函数array_push()、array_pop()及array_shift()简单用法示例
2020/01/26 PHP
javascript 写类方式之一
2009/07/05 Javascript
jqPlot Option配置对象详解
2009/07/25 Javascript
javaScript call 函数的用法说明
2010/04/09 Javascript
关于Ext中form移除textfield方法:hide(),setVisible(false),remove()
2010/12/02 Javascript
JavaScript中实现单体模式分享
2015/01/29 Javascript
AngularJS使用angular-formly进行表单验证
2015/12/27 Javascript
JavaScript中的this,call,apply使用及区别详解
2016/01/29 Javascript
归纳下js面向对象的几种常见写法总结
2016/08/24 Javascript
js判断是否为空和typeof的用法(详解)
2016/10/07 Javascript
js自制图片放大镜功能
2017/01/24 Javascript
原生js实现可拖拽效果
2017/02/28 Javascript
实现微信小程序的wxml文件和wxss文件在webstrom的支持
2017/06/12 Javascript
node.js 核心http模块,起一个服务器,返回一个页面的实例
2017/09/11 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
js刷新页面location.reload()用法详解
2019/12/09 Javascript
[50:22]完美盛典-2018年度红毯走秀
2018/12/16 DOTA
[01:08:24]DOTA2-DPC中国联赛 正赛 RNG vs Phoenix BO3 第一场 2月5日
2021/03/11 DOTA
小结Python用fork来创建子进程注意事项
2014/07/03 Python
Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解
2018/09/04 Python
python实现桌面托盘气泡提示
2019/07/29 Python
关于python字符串方法分类详解
2019/08/20 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
2020/04/27 Python
CSS3 3D酷炫立方体变换动画的实现
2019/03/26 HTML / CSS
水污染治理专业毕业生推荐信
2013/11/14 职场文书
护理专业自我鉴定
2014/01/30 职场文书
小学生开学感言
2014/02/28 职场文书
三严三实对照检查材料思想汇报
2014/09/28 职场文书
群众路线专项整治工作情况报告
2014/10/28 职场文书