如何在存储过程中使用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开发的一般流程
Aug 13 面试题
怎样在程序里获得一个空指针
Jan 24 面试题
数据库专业英语
Nov 30 面试题
一套.net面试题及答案
Nov 02 面试题
程序员机试试题汇总
Mar 07 面试题
什么是URL
Dec 13 面试题
面向对象设计的原则是什么
Feb 13 面试题
介绍一下mysql的日期和时间函数
Mar 28 面试题
shell变量的作用空间是什么
Aug 17 面试题
四种会话跟踪技术
May 20 面试题
介绍一下Ruby的多线程处理
Feb 01 面试题
请介绍一下WSDL的文档结构
Mar 17 面试题
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
Joomla实现组件中弹出一个模式(modal)窗口的方法
2016/05/04 PHP
PHP将URL转换成短网址的算法分享
2016/09/13 PHP
php文件类型MIME对照表(比较全)
2016/10/07 PHP
php基于dom实现读取图书xml格式数据的方法
2017/02/03 PHP
JavaScript 原型链学习总结
2010/10/29 Javascript
Javascript学习笔记之 函数篇(二) : this 的工作机制
2014/06/24 Javascript
javascript父、子页面交互技巧总结
2014/08/08 Javascript
JavaScript实现仿网易通行证表单验证
2015/05/25 Javascript
利用Node.js+Koa框架实现前后端交互的方法
2017/02/27 Javascript
js实现产品缩略图效果
2017/03/10 Javascript
JS实现列表页面隔行变色效果
2017/03/25 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
浅谈Angular7 项目开发总结
2018/12/19 Javascript
Vue的生命周期操作示例
2019/09/17 Javascript
javascript实现计算器功能
2020/03/30 Javascript
Javascript call及apply应用场景及实例
2020/08/26 Javascript
vue项目打包为APP,静态资源正常显示,但API请求不到数据的操作
2020/09/12 Javascript
原生JavaScript实现换肤
2021/02/19 Javascript
Python列表切片用法示例
2017/04/19 Python
python中装饰器级连的使用方法示例
2017/09/29 Python
Python实现的基数排序算法原理与用法实例分析
2017/11/23 Python
Python使用Turtle模块绘制五星红旗代码示例
2017/12/11 Python
python实现泊松图像融合
2018/07/26 Python
TensorFlow实现模型评估
2018/09/07 Python
Python用Try语句捕获异常的实例方法
2019/06/26 Python
python判断变量是否为int、字符串、列表、元组、字典的方法详解
2020/02/13 Python
Python多个装饰器的调用顺序实例解析
2020/05/22 Python
雅诗兰黛香港官网:Estee Lauder香港
2017/09/26 全球购物
美国工业用品采购网站:Zoro.com
2020/10/27 全球购物
初入社会应届生求职信
2013/11/18 职场文书
家长对小学生的评语
2014/01/28 职场文书
我的画教学反思
2014/04/28 职场文书
应届生自荐书
2014/06/23 职场文书
关于颐和园的导游词
2015/01/30 职场文书
如何书写读后感?(附范文)
2019/07/26 职场文书
原型和原型链 prototype和proto的区别详情
2021/11/02 Javascript