易截截图软件、单文件、免安装、纯绿色、仅160KB

Mysql的游标究竟怎么用–映日荷花别样红


Mysql的游标究竟怎么用–映日荷花别样红
Mysql从5.0开始支持存储过程和trigger,给我们喜欢用mysql的朋友们更喜欢mysql的理由了,语法
上和PL/SQL有差别,不过搞过编程的人都知道,语法不是问题,关键是思想,大致了解语法后,就从
变量定义,循环,判断,游标,异常处理这个几个方面详细学习了。关于游标的用法Mysql现在提供
的还很特别,虽然使用起来没有PL/SQL那么顺手,不过使用上大致上还是一样,
定义游标
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;
使用游标
open fetchSeqCursor;
fetch数据
fetch cursor into _seqname, _value;
关闭游标
close fetchSeqCursor;
不过这都是针对cursor的操作而已,和PL/SQL没有什么区别吧,不过光是了解到这个是根本不足以
写出Mysql的fetch过程的,还要了解其他的更深入的知识,我们才能真正的写出好的游标使用的proc
edure
首先fetch离不开循环语句,那么先了解一下循环吧。
我一般使用Loop和while觉得比较清楚,而且代码简单。
这里使用Loop为例
fetchSeqLoop:Loop
fetch cursor into _seqname, _value;
end Loop;
现在是死循环,还没有退出的条件,那么在这里和oracle有区别,Oracle的PL/SQL的指针有个隐性变
量%notfound,Mysql是通过一个Error handler的声明来进行判断的,
declare continue handler for Not found (do some action);
在Mysql里当游标遍历溢出时,会出现一个预定义的NOT FOUND的Error,我们处理这个Error并定义一
个continue的handler就可以叻,关于Mysql Error handler可以查询Mysql手册
定义一个flag,在NOT FOUND,标示Flag,在Loop里以这个flag为结束循环的判断就可以叻。
declare fetchSeqOk boolean; ## define the flag for loop judgement
declare _seqname varchar(50); ## define the varient for store the data
declare _value bigint(20);
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;## define the cursor
declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for not
found flag
set fetchSeqOk = false;
open fetchSeqCursor;
fetchSeqLoop:Loop
if fetchSeqOk then
leave fetchSeqLoop;
else
fetch cursor into _seqname, _value;
select _seqname, _value;
end if;
end Loop;
close fetchSeqCur


相关文档:

MySQL数据库备份和还原的常用命令


备份MySQL数据库的命令
mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql
备份MySQL数据库为带删除表的格式
备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库。
mysqldump -–add-drop-table -uusername -ppassword databasename > bac ......

[Injection]对MYSQL 5.0服务器以上版本注入


by ZaraByte
How to do a SQL Injection for MYSQL Server 5.0+
1. Find a vulnerable add a ‘ at the end of the site example: news.php?id=1 add a ‘ at the end of the 1 and see if you get a syntax error
2. order by #–
Keep upping the # until you get an error.
3. union all select 1 ......

Java调用存储过程(MySql数据库)

一、建表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` varchar(16) NOT NULL default '',
  `REMARK` varchar(16) NOT NULL default '',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
&nb ......

MySQL学习

基本的MySQL语句很简单,这里主要谈谈一些容易遗忘的。
1.如何设置字段递增
create table tb_User(Id int auto_increment
not null primary key,UserName varchar(50),Password varchar(20));
2.查看表结构
desc tb_User;
3.如何修改表结
重命名表:alter table tb_User rename
tb_UserInfo;
添加一列:alter ta ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号