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

面试题 相关文章推荐
NULL是什么,它是怎么定义的
May 09 面试题
为什么group by 和order by会使查询变慢
May 16 面试题
ASP.NET中的身份验证有那些
Jul 13 面试题
关于VPN
Jun 10 面试题
介绍一下ICMP(Internet Control Message Protocol)Internet控制信息协议
Nov 26 面试题
网络体系结构及协议的定义
Mar 13 面试题
50道外企软件测试面试题
Aug 18 面试题
介绍一下XMLHttpRequest对象
Feb 12 面试题
如何利用XMLHTTP检测URL及探测服务器信息
Nov 10 面试题
GWT都有什么特性
Dec 02 面试题
如何提高MySql的安全性
Jun 19 面试题
DTD的含义以及作用
Jan 26 面试题
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浮点数精确运算
2016/03/10 PHP
php基于session锁防止阻塞请求的方法分析
2017/08/07 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
ThinkPHP中获取指定日期后工作日的具体日期方法
2018/10/14 PHP
最近项目写了一些js,水平有待提高
2009/01/31 Javascript
28个JS验证函数收集
2010/03/02 Javascript
jquery控制listbox中项的移动并排序的实现代码
2010/09/28 Javascript
Javascript根据指定下标或对象删除数组元素
2012/12/21 Javascript
用JS在浏览器中创建下载文件
2014/03/05 Javascript
JsRender for index循环索引用法详解
2014/10/31 Javascript
AngularJS中实现显示或隐藏动画效果的方式总结
2015/12/31 Javascript
React快速入门教程
2017/01/17 Javascript
Express+Nodejs 下的登录拦截实现代码
2017/07/01 NodeJs
JavaScript中关于class的调用方法
2017/11/28 Javascript
JS解析后台返回的JSON格式数据实例
2018/08/06 Javascript
vue 优化CDN加速的方法示例
2018/09/19 Javascript
jquery获取img的src值实例介绍
2019/01/16 jQuery
简单实现vue中的依赖收集与响应的方法
2019/02/18 Javascript
package.json配置文件构成详解
2019/08/27 Javascript
Vue绑定用户接口实现代码示例
2020/11/04 Javascript
[56:13]DOTA2-DPC中国联赛定级赛 LBZS vs Phoenix BO3第一场 1月10日
2021/03/11 DOTA
详谈python read readline readlines的区别
2017/09/22 Python
Python根据成绩分析系统浅析
2019/02/11 Python
Tensorflow模型实现预测或识别单张图片
2019/07/19 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
2020/11/18 Python
美国在线眼镜店:GlassesShop
2018/11/15 全球购物
法国亚马逊官方网站:Amazon.fr
2020/12/19 全球购物
求职简历中自我评价
2014/01/28 职场文书
保安队长职务说明书
2014/02/23 职场文书
餐厅采购员岗位职责
2014/03/06 职场文书
品牌推广策划方案
2014/05/28 职场文书
一份没有按时交货失信于客户的检讨书
2014/09/19 职场文书
中标通知书
2015/04/17 职场文书
幼儿园教师辞职信
2019/06/21 职场文书
go语言中fallthrough的用法说明
2021/05/06 Golang
python内置进制转换函数的操作
2021/06/02 Python