-- cursor 游标
/*
declare 声明; declare 游标名 cursor for select_statement;
open 找开; open 游标名
fetch 取值; fetch 游标名 into var1,var2,var3[,...]
close 关闭; close 游标名;
*/
create procedure p12()
begin
declare row_gid int;
declare row_num int;
declare row_name varchar(20);

declare getgoods cursor for select gid,num,name from goods;

open getgoods;
fetch getgoods into row_gid,row_num,row_name;
select row_num,row_name;
close getgoods;
end;

create procedure p13()
begin
declare cnt int default 0;
declare i int default 0 ;
declare row_gid int;
declare row_num int;
declare row_name varchar(20);

declare getgoods cursor for select gid,num,name from goods;

select count(*) into cnt from goods;

open getgoods;

repeat
set i := i+1;
fetch getgoods into row_gid,row_num,row_name;
select row_num,row_name;
until i>=cnt end repeat;

close getgoods;
end;

--利用标误码来结束循环
--在mysql cursor中,可以把declare continue handler来操作1个越界标识
/*
declare continue handler for not found 语句;
*/
create procedure p14()
begin
declare row_gid int;
declare row_num int;
declare row_name varchar(20);
declare flag int default 1; --标识

declare getgoods cursor for select gid,num,name from goods;

declare continue handler for NOT FOUND set flag := 0;

open getgoods;

repeat
if flag!=0 then
fetch getgoods into row_gid,row_num,row_name;
select row_num,row_name;
end if;
until flag=0 end repeat;
close getgoods;
end;

Mysql游标的简明写法的更多相关文章

  1. SqlServer和MySQL游标学习

    一 sqlserver游标使用 /*** 游标的使用  讲了这个多游标的优点,现在我们就亲自来揭开游标的神秘的面纱.  使用游标的顺序: 声名游标.打开游标.读取数据.关闭游标.删除游标. 1.3.1 ...

  2. MySQL游标操作指南

    本篇文章是对MySQL游标的具体使用进行了详细的分析介绍,需要的朋友参考下   测试表 level  代码如下: create table test.level (name varchar(20)); ...

  3. php中mysql语句的基本写法

    php中mysql语句的基本写法 php作为一门后台语言必须要与mysql数据库打交道,做到将内容存储到数据库以及数据库数据读写的操作,那么下面就来说下最近学习的一些东西: 在具体将之前先说一下编码的 ...

  4. 个人笔记mysql游标

    经过测试,mysql游标是无法读取自定义函数计算的结构,mysql自带的函数计算值是可以读取的.

  5. MySQL游标的简单实践

    Q:为什么要使用游标? A: 在存储过程(或函数)中,如果某条select语句返回的结果集中只有1行,可以使用select into语句(上几篇博客有介绍到用法)来得到该行进行处理:如果结果集中有多行 ...

  6. mysql 游标 demo

    一.MySQL游标的概念 1.游标介绍 MySQL的游标(cursor)是一个重要的概念,通过查找资料与自己的理解,主要得出以下几点关于自己的理解. 有数据缓冲的思想:游标的设计是一种数据缓冲区的思想 ...

  7. springboot成神之——springboot+mybatis+mysql搭建项目简明demo

    springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...

  8. [转]MySQL游标的使用

    转自:http://www.cnblogs.com/sk-net/archive/2011/09/07/2170224.html 以下的文章主要介绍的是MySQL游标的使用笔记,其可以用在存储过程的S ...

  9. Mysql 游标初识

    MySql 游标初识 认识 游标(cursor), 按字面意思可理解为, 游动的标识, 或者叫做"光标", 这样更容易理解. 就好比现有一张表存储了n行记录, 然后我想每次取出一行 ...

随机推荐

  1. root密码

    安装完Ubuntu后忽然意识到没有设 置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的 root密码.我 ...

  2. leetcode 101 Symmetric Tree ----- java

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. JavaScript学习记录总结(六)——js函数闭包特性

    <script type="text/javascript">    function arrdemo(){        var arr=["hello&q ...

  4. STL概述

    一.关于STL STL(Standard Template Library,标准模板库)是C++语言标准中的重要组成部分.STL 以模板类和模板函数的形式为程序员提供了各种数据结构和算法的精巧实现,程 ...

  5. IAR MSP430如何生成烧写文件

    IAR生成430烧写方法有2种, 第一种是:将工程的debug模式切换成release模式,看图片操作.    那个.d43文件就是仿真调试模式的文件. 这里的test.txt文件就是烧写文件了,不要 ...

  6. Fixing Poor MySQL Default Configuration Values

    I've recently been accumulating some MySQL configuration variables that have defaults which have pro ...

  7. Tkinter

    单个选项 from Tkinter import * root = Tk() v = IntVar() Radiobutton(root, text="One", variable ...

  8. syslog-ng 安装

    下载 Syslog-NG的rpm包,  地址 http://www.kevindeng.org/wp-content/uploads/2010/10/Syslog-NG.zip unzip解压 [ro ...

  9. MVC View中获取Url参数的值

    如果url是 /home/index?id=3 直接Request就ok. Razor方法 @Html.ViewContext.RouteData.Values["id"] @Re ...

  10. prtg

    prtg http://www.paessler.com/prtg/features prtg的sensor技术 数据库监视 Flexible Alerting 9 notification tech ...