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

面试题 相关文章推荐
链表面试题-一个链表的结点结构
May 04 面试题
一道SQL存储过程面试题
Oct 07 面试题
触发器(trigger)的功能都有哪些?写出一个触发器的例子
Sep 17 面试题
ASP.NET中的身份验证有那些
Jul 13 面试题
广州盈通面试题
Dec 05 面试题
STP的判定过程
Oct 01 面试题
介绍一下MD5加密算法
Nov 12 面试题
linux比较文件内容的命令是什么
Mar 04 面试题
JSP和EJB可以共享HttpSession么?EJB里面可以改变session里面的内容
Jun 05 面试题
如何写出高质量、高性能的MySQL查询
Nov 17 面试题
final, finally, finalize的区别
Mar 01 面试题
简单的JAVA编程面试题
Mar 19 面试题
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
vBulletin HACK----关于排版的两个HACK
2006/10/09 PHP
php写的AES加密解密类分享
2014/06/20 PHP
使用PHP和HTML5 FormData实现无刷新文件上传教程
2014/09/06 PHP
php将金额数字转化为中文大写
2015/07/09 PHP
使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能示例
2017/09/15 PHP
lnmp安装多版本PHP共存的方法详解
2018/08/02 PHP
解决laravel资源加载路径设置的问题
2019/10/14 PHP
javascript 选择文件夹对话框(web)
2009/07/07 Javascript
javaScript NameSpace 简单说明介绍
2013/07/18 Javascript
jQuery支持动态参数将函数绑定到事件上的方法
2015/03/17 Javascript
readonly和disabled属性的区别
2015/07/26 Javascript
vue2.0开发实践总结之疑难篇
2016/12/07 Javascript
原生javascript实现图片放大镜效果
2017/01/18 Javascript
jQuery加载及解析XML文件的方法实例分析
2017/01/22 Javascript
详解vue2 $watch要注意的问题
2017/09/08 Javascript
js仿微信抢红包功能
2020/09/25 Javascript
浅析Vue自定义组件的v-model
2017/11/26 Javascript
手把手教你使用TypeScript开发Node.js应用
2019/05/06 Javascript
vue实现配置全局访问路径头(axios)
2019/11/01 Javascript
解决基于 keep-alive 的后台多级路由缓存问题
2020/12/23 Javascript
简单易懂的python环境安装教程
2017/07/13 Python
Python 实现在文件中的每一行添加一个逗号
2018/04/29 Python
pycharm 将django中多个app放到同个文件夹apps的处理方法
2018/05/30 Python
详解Python3中的迭代器和生成器及其区别
2018/10/09 Python
PyQt Qt Designer工具的布局管理详解
2019/08/07 Python
python使用协程实现并发操作的方法详解
2019/12/27 Python
Python实现搜索算法的实例代码
2020/01/02 Python
pytorch之添加BN的实现
2020/01/06 Python
TripAdvisor印尼站:全球领先的旅游网站
2018/03/15 全球购物
美国性感内衣店:Yandy
2018/06/12 全球购物
和睦家庭事迹
2014/05/14 职场文书
党的群众路线教育实践活动专题组织生活会发言材料
2014/10/17 职场文书
升职自荐信范文
2015/03/27 职场文书
金正昆讲礼仪观后感
2015/06/11 职场文书
暂住证证明
2015/06/19 职场文书
决心书格式及范文
2019/06/24 职场文书