1.游标的概念

有时,需要在检索出来的行中前进或后退一行或多行。这就是使用游标的原因。游标(cursor)是一个存储在 MySql 服务器上的数据库查询,它不是一条 select 语句,而是被该语句检索出来的结果集。在存储了游标之后,应用程序可以根据需要滚动或浏览其中的数据。

主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或做出更改。

MySql 游标只能用于存储过程和函数。


2.使用游标的步骤

在能够使用游标前,必须定义它。这个过程实际上没有检索数据,它只是定义要使用的 select 语句;

一旦声明后,必须打开游标以供使用。这个过程用前面定义的 select 语句把数据实际检索出来;

对于填有数据的游标,根据需要取出各行;

在游标结束使用时,必须关闭游标。


3.创建游标

游标用 declare 语句创建。

declare 命名游标,并定义相应的 select 语句,根据需要带 where 和其它子句。

create procedure processorder()

 begin    

  declare ordernumbers cursor   

   for    

  select value from test; 

end;

4.打开和关闭游标

open ordernumbers; --打开 

close orderbumbers; --关闭

5.使用游标数据

在一个游标被打开后,可以使用 fetch 语句分别访问它的每一行。

fetch 指定检索什么数据,检索出来的数据存储在什么地方,它还向前移动游标中的内部行指针,使下一条 fetch 语句检索下一行(不重复读取同一行)。

create procedure procders()
begin
--定义一个布尔变量 done 和 一个整型变量 o
declare done boolean default 0;
declare o int; --定义一个游标
declare testYB cursor
for
select value test; --循环
declare continue handler for sqlstate '' set done=1; --创建一张表
create table if not exists test1(value text); --打开游标
open testYB; repeat --读取到 o 中
fetch testYB into o; --把 o 中的数据添加到 test1 表中
insert into test1(value) values(o); until done end repeat; --关闭游标
close testYB;
end;

最新文章

  1. CentOS 6.5安装Node.js, npm
  2. Linux安全基础:grep命令的使用
  3. ubuntu上搭建review board代码评审站点
  4. excel相关
  5. Android优秀资源整理合集(论菜鸟到高级攻城狮)
  6. 如何恢复SQL Server 中的Master库
  7. SAP财务凭证冲销
  8. Creating a Background Service ——IntentService
  9. [Angular 2] Passing Observables into Components with Async Pipe
  10. 玩转Firefox侧栏
  11. Magento布局layout.xml文件详解
  12. bash:xxx:command not found
  13. Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextAware
  14. EasyPR源码剖析(7):车牌判断之SVM
  15. 处理返回键劫持(结合vue)
  16. JAVA中各种日期表示字母
  17. js基础知识:闭包,事件处理,原型
  18. DataTable进行排序Asc升序,Desc降序
  19. [ IE浏览器兼容问题 ] Web Uploader 在IE、FireFox下点击上传没反应
  20. python队列、线程、进程、协程

热门文章

  1. gstreamer让playbin能够播放rtp over udp流数据
  2. line-hight-(行高)解析
  3. js参数截取
  4. poj2385 简单DP
  5. 关于extern对变量的使用
  6. Js与flash交互:在html页面中用js与MyReport插件交互
  7. 获取本机CPU,硬盘等使用情况
  8. 文成小盆友python-num2 数据类型、列表、字典
  9. Big Event in HDU(HDU 1171 多重背包)
  10. js代码中的parent,top和self有什么区别