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

面试题 相关文章推荐
以下的初始化有什么区别
Dec 16 面试题
下面关于"联合"的题目的输出是什么
Aug 06 面试题
查询优化的一般准则有哪些
Mar 08 面试题
局域网定义和特性
Jan 23 面试题
逻辑链路控制协议
Oct 01 面试题
如何保障Web服务器安全
May 05 面试题
C#面试常见问题
Feb 25 面试题
C# Debug和Testing相关面试题
Oct 25 面试题
实体的生命周期
Aug 31 面试题
线程同步的方法
Nov 23 面试题
ruby如何进行集成操作?Ruby能进行多重继承吗?
Oct 16 面试题
SOA面试题:如何在SOA中实现松耦合
Jul 21 面试题
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的一个基础知识 表单提交
2011/07/04 PHP
深入了解PHP类Class的概念
2012/06/14 PHP
解析PHP中的正则表达式以及模式匹配
2013/06/19 PHP
多个PHP中文字符串截取函数
2013/11/12 PHP
php根据某字段对多维数组进行排序的方法
2015/03/07 PHP
教大家制作简单的php日历
2015/11/17 PHP
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
2015/12/21 PHP
PHP微信公众号开发之微信红包实现方法分析
2017/07/14 PHP
Javascript 更新 JavaScript 数组的 uniq 方法
2008/01/23 Javascript
javascript 混合的构造函数和原型方式,动态原型方式
2009/12/07 Javascript
JS+Canvas绘制时钟效果
2020/08/20 Javascript
JavaScript设计模式之代理模式实例分析
2019/01/16 Javascript
vue写h5页面的方法总结
2019/02/12 Javascript
jQuery选择器之基本过滤选择器用法实例分析
2019/02/19 jQuery
通过javascript实现段落的收缩与展开
2019/06/26 Javascript
JS前端面试必备——基本排序算法原理与实现方法详解【插入/选择/归并/冒泡/快速排序】
2020/02/24 Javascript
仿照Element-ui实现一个简易的$message方法
2020/09/14 Javascript
JavaScript实现简易计算器小功能
2020/10/22 Javascript
python统计文本文件内单词数量的方法
2015/05/30 Python
tornado 多进程模式解析
2018/01/15 Python
python实现图片批量压缩程序
2018/07/23 Python
django主动抛出403异常的方法详解
2019/01/04 Python
python字典setdefault方法和get方法使用实例
2019/12/25 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
新加坡时尚网上购物:Zalora新加坡
2016/07/26 全球购物
DHC中国官方购物网站:日本通信销售No.1化妆品
2016/08/20 全球购物
Melijoe英国官网:法国儿童时尚网站
2016/11/18 全球购物
澳洲网红粉泥面膜:Sand & Sky
2019/08/13 全球购物
软件设计的目标是什么
2016/12/04 面试题
财务内勤岗位职责
2014/04/17 职场文书
学校组织向国旗敬礼活动方案(中小学适用)
2014/09/27 职场文书
三八节活动主持词
2015/07/04 职场文书
2015教师个人德育工作总结
2015/07/22 职场文书
教你如何用python开发一款数字推盘小游戏
2021/04/14 Python
Python实现PIL图像处理库绘制国际象棋棋盘
2021/07/16 Python
CSS 鼠标点击拖拽效果的实现代码
2022/12/24 HTML / CSS