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

面试题 相关文章推荐
怎样声明接口
Sep 19 面试题
寻找迷宫的一条出路,o通路;X:障碍
Jul 10 面试题
写一个函数返回1+2+3+…+n的值(假定结果不会超过长整型变量的范围)
Sep 05 面试题
Android面试题及答案
Sep 04 面试题
什么是TCP/IP
Jul 27 面试题
用C#语言写出与SQLSERVER访问时的具体过程
Apr 16 面试题
如何利用find命令查找文件
Nov 18 面试题
27个经典Linux面试题及答案,你知道几个?
Jan 10 面试题
启动一个线程是用run()还是start()
Dec 25 面试题
关于Java finally的面试题
Apr 27 面试题
请说出以下代码输出什么
Aug 30 面试题
Java Servlet的主要功能和作用是什么
Feb 14 面试题
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
1 Tube Radio
2021/03/02 无线电
php完全过滤HTML,JS,CSS等标签
2009/01/16 PHP
php防攻击代码升级版
2010/12/29 PHP
PHP获取客户端真实IP地址的5种情况分析和实现代码
2014/07/08 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
解决Laravel 使用insert插入数据,字段created_at为0000的问题
2019/10/11 PHP
slice函数的用法 之不错的应用
2006/12/29 Javascript
JavaScript作用域链使用介绍
2013/08/29 Javascript
一个简单的实现下拉框多选的插件可移植性比较好
2014/05/05 Javascript
jQuery往textarea中光标所在位置插入文本的方法
2015/06/26 Javascript
JavaScript简单获取页面图片原始尺寸的方法
2016/06/21 Javascript
原生js代码实现图片放大境效果
2016/10/30 Javascript
Javascript 实现计算器时间功能详解及实例(二)
2017/01/08 Javascript
详解VueJs异步动态加载块
2017/03/09 Javascript
用原生JS实现简单的多选框功能
2017/06/12 Javascript
JavaScript Date对象应用实例分享
2017/10/30 Javascript
vue+webpack模拟后台数据的示例代码
2018/07/26 Javascript
微信小程序实现保存图片到相册功能
2018/11/30 Javascript
node后端服务保活的实现
2019/11/10 Javascript
使用Python的Twisted框架编写非阻塞程序的代码示例
2016/05/25 Python
python使用两种发邮件的方式smtp和outlook示例
2017/06/02 Python
Win10下python 2.7.13 安装配置方法图文教程
2018/09/18 Python
Django框架创建项目的方法入门教程
2019/11/04 Python
Python3.x+迅雷x 自动下载高分电影的实现方法
2020/01/12 Python
Python 解析简单的XML数据
2020/07/24 Python
python通过cython加密代码
2020/12/11 Python
CSS3制作翻转效果_动力节点Java学院整理
2017/07/11 HTML / CSS
贝斯特韦斯特酒店集团官网:Best Western
2019/01/03 全球购物
入学生会自荐书范文
2014/02/05 职场文书
2014年接待工作总结
2014/11/26 职场文书
撤诉申请怎么写
2015/05/19 职场文书
文艺演出主持词
2015/07/01 职场文书
推普标语口号大全
2015/12/26 职场文书
银行岗位培训心得体会
2016/01/09 职场文书
vue二维数组循环嵌套方式 循环数组、循环嵌套数组
2022/04/24 Vue.js
instantclient客户端 连接oracle数据库
2022/04/26 Oracle