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

面试题 相关文章推荐
JDK安装目录下有哪些内容
Aug 25 面试题
JAVA代码查错题
Oct 10 面试题
估算杭州有多少软件工程师
Aug 11 面试题
C++:memset ,memcpy和strcpy的根本区别
Apr 27 面试题
SQL Server的固定数据库角色都有哪些?对应的服务器权限有哪些?
May 18 面试题
存储过程和sql语句的优缺点
Jul 02 面试题
什么是TCP/IP
Jul 27 面试题
C#面试题
May 06 面试题
GWebs公司笔试题
May 04 面试题
客服端调用EJB对象的几个基本步骤
Jan 15 面试题
Prototype中如何为一个元素添加一个方法
Dec 08 面试题
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
php提示Call-time pass-by-reference has been deprecated in的解决方法[已测]
2012/05/06 PHP
初步介绍PHP扩展开发经验分享
2012/09/06 PHP
提升PHP性能的21种方法介绍
2013/06/25 PHP
请离开include_once和require_once
2013/07/18 PHP
ThinkPHP框架实现的MySQL数据库备份功能示例
2018/05/24 PHP
javascript入门·动态的时钟,显示完整的一些方法,新年倒计时
2007/10/01 Javascript
js继承的实现代码
2010/08/05 Javascript
Javascript new Date().valueOf()的作用与时间戳由来详解
2013/04/24 Javascript
JavaScript计算字符串中每个字符出现次数的小例子
2013/07/02 Javascript
jQuery中使用Ajax获取JSON格式数据示例代码
2013/11/26 Javascript
Javascript设计模式之观察者模式(推荐)
2016/03/29 Javascript
jQueryUI DatePicker 添加时分秒
2016/06/04 Javascript
JS 拦截全局ajax请求实例解析
2016/11/29 Javascript
vue实现动态添加数据滚动条自动滚动到底部的示例代码
2018/07/06 Javascript
webpack + vue 打包生成公共配置文件(域名) 方便动态修改
2019/08/29 Javascript
微信小程序后端无法保持session的原因及解决办法问题
2020/03/20 Javascript
vue 解决兄弟组件、跨组件深层次的通信操作
2020/07/27 Javascript
[01:48]2018DOTA2亚洲邀请赛主赛事第二日五佳镜头 VG完美团战逆转TNC
2018/04/05 DOTA
Python正则表达式教程之一:基础篇
2017/03/02 Python
python生成随机图形验证码详解
2017/11/08 Python
说说如何遍历Python列表的方法示例
2019/02/11 Python
利用python中集合的唯一性实现去重
2020/02/11 Python
Canvas多边形绘制的实现方法
2019/08/05 HTML / CSS
中国第一家杂志折扣订阅网:杂志铺
2016/08/30 全球购物
Calphalon美国官网:美国顶级锅具品牌
2020/02/05 全球购物
我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
2014/03/30 面试题
中小学生学籍证明
2014/10/25 职场文书
护士自我推荐信范文
2015/03/24 职场文书
龙猫观后感
2015/06/09 职场文书
总结会主持词
2015/07/02 职场文书
先进个人主要事迹范文
2015/11/04 职场文书
Nginx设置日志打印post请求参数的方法
2021/03/31 Servers
python数据分析之用sklearn预测糖尿病
2021/04/22 Python
golang 实现对Map进行键值自定义排序
2021/04/28 Golang
SQL基础查询和LINQ集成化查询
2022/01/18 MySQL
Python IO文件管理的具体使用
2022/03/20 Python