Mysql那些事儿之(十四)光标的使用_MySQL
bitsCN.com
Mysql那些事儿之(十四)光标的使用
相关链接:
Mysql那些事儿之(一)mysql的安装
http:///database/201210/162314.html;
Mysql那些事儿之(二)有关数据库的操作
http:///database/201210/162315.html;
Mysql那些事儿之(三)有关数据表的操作
http:///database/201210/162316.html;
Mysql那些事儿之(四)数据表数据查询操作
http:///database/201210/162317.html;
Mysql那些事儿之(五)操作时间
http:///database/201210/162318.html;
Mysql那些事儿之(六)字符串模式匹配
http:///database/201210/163969.html;
Mysql那些事儿之(七)深入select查询
http:///database/201210/163970.html;
Mysql那些事儿之(八)索引
http:///database/201210/163971.html;
Mysql那些事儿之(九)常用的函数
http:///database/201210/164229.html;
Mysql那些事儿之(十)触发器一
http:///database/201210/164516.html;
Mysql那些事儿之(十一)触发器二
http:///database/201210/164766.html;
Mysql那些事儿之(十二)存储过程
http:///database/201210/164795.html;
Mysql那些事儿之(十三)变量、条件的使用
http:///database/201211/165662.html
在存储过程中可以使用光标对结果集进行循环处理,光标的使用包括光标的声明、open、fetch、close。
语法如下:
Sql代码
--声明光标
DECLARE cur_name CURSOR FOR select_statement
--open光标
OPEN cursor_name
--FETCH 光标
FETCH cursor_name INTO var_name [,var_name....]
--close光标
CLOSE cursor_name
举例说明:
Sql代码
delimiter $$ --将;结束符改变为$$
--创建存储过程
CREATE PROCEDURE payment_amount()
BEGIN
---声明变量
DECLARE i_staff_id int;
DECLARE d_amount decimal(5,2);
---声明一个光标,获取表payment里的staff_id,amount列的值。
DECLARE cur_payment cursor for select staff_id,amount from payment;
---条件处理,判断循环结束的条件是 捕获NOT FOUND条件。
---当fecth 找不到下一条记录时,就会关闭光标,退出过程。
DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
set @x1 = 0;
set @x2 = 0;
----打开光标
OPEN cur_payment;
REPEAT
FETCH cur_payment INTO i_staff_id,d_amount;
if i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end if;
UNTIL 0 END REPEAT;
CLOSE cur_payment;
END;
$$
delimiter ;
DECLARE定义是有顺序的:变量和条件必须放在前面、然后是光标的声明、最后才可以是 处理程序的声明。
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
