Home Database Mysql Tutorial Mysql那些事儿之(十四)光标的使用_MySQL

Mysql那些事儿之(十四)光标的使用_MySQL

Jun 02, 2016 am 08:49 AM
database html

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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles