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

面试题 相关文章推荐
Weblogc domain问题
Jan 27 面试题
函数指针的定义是什么
Aug 14 面试题
const char*, char const*, char*const的区别是什么
Jul 09 面试题
为什么UNION ALL比UNION快
Mar 17 面试题
怎么可以提高数据库查询数据的速度
Jun 28 面试题
请解释virtual关键字的含义
Jun 17 面试题
开放系统互连参考模型
Jun 29 面试题
override和overload的区别
Mar 09 面试题
Linux如何为某个操作添加别名
Feb 05 面试题
法雷奥SQA(electric)面试问题
Jan 23 面试题
MYSQL基础面试题
May 13 面试题
java程序员面试交流
Nov 29 面试题
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
一个简单的自动发送邮件系统(二)
2006/10/09 PHP
php类中的各种拦截器用法分析
2014/11/03 PHP
php+mysql查询实现无限下级分类树输出示例
2016/10/03 PHP
利用JavaScript检测CPU使用率自己写的
2014/03/22 Javascript
javascript中的nextSibling使用陷(da)阱(keng)
2014/05/05 Javascript
JavaScript实现函数返回多个值的方法
2015/06/09 Javascript
基于jQuery实现以手风琴方式展开和折叠导航菜单
2016/01/28 Javascript
JavaScript设计模式经典之工厂模式
2016/02/24 Javascript
Javascript实现前端简单的路由实例
2016/09/11 Javascript
微信小程序 下拉列表的实现实例代码
2017/03/08 Javascript
vue.js中指令Directives详解
2017/03/20 Javascript
javascript实现多张图片左右无缝滚动效果
2017/03/22 Javascript
ES6(ECMAScript 6)新特性之模板字符串用法分析
2017/04/01 Javascript
json的结构与遍历方法实例分析
2017/04/25 Javascript
JavaScript中错误正确处理方式小结你用对了吗
2017/10/10 Javascript
微信小程序如何像vue一样在动态绑定类名
2018/04/17 Javascript
JS实现图片拖拽交换效果
2018/11/30 Javascript
ckeditor一键排版功能实现方法分析
2020/02/06 Javascript
[03:12]完美世界DOTA2联赛PWL DAY7集锦
2020/11/06 DOTA
python过滤字符串中不属于指定集合中字符的类实例
2015/06/30 Python
python3实现随机数
2018/06/25 Python
Python远程视频监控程序的实例代码
2019/05/05 Python
创建Django项目图文实例详解
2019/06/06 Python
解决jupyter notebook 前面书写后面内容消失的问题
2020/04/13 Python
基于PyInstaller各参数的含义说明
2021/03/04 Python
幼儿园门卫岗位职责
2014/02/14 职场文书
公司经理聘任书
2014/03/29 职场文书
求职信内容怎么写
2014/05/26 职场文书
机关单位工作失职检讨书
2014/11/20 职场文书
2014年房产经纪人工作总结
2014/12/08 职场文书
户外拓展训练感想
2015/08/07 职场文书
2016暑期政治学习心得体会
2016/01/23 职场文书
日本读研:怎样写好一篇日本研究计划书?
2019/07/15 职场文书
蔬果开业典礼发言稿应该怎么写?
2019/09/03 职场文书
Flask使用SQLAlchemy实现持久化数据
2021/07/16 Python
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL