码迷,mamicode.com
首页 > 其他好文 > 详细

简单存储过程+游标

时间:2016-06-01 12:49:34      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

CREATE TABLE `pr_user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user` varchar(255) DEFAULT NULL,
`dp` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

 

CREATE TABLE `pr_user_tmp` (
`user` varchar(255) DEFAULT NULL,
`dp` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

DROP PROCEDURE IF EXISTS sys_user ;
CREATE PROCEDURE sys_user()
BEGIN
DECLARE done int;
DECLARE u VARCHAR(255);
DECLARE d VARCHAR(255);
DECLARE u_c CURSOR FOR SELECT user u,dp d from pr_user ORDER BY id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN u_c;
c:LOOP
FETCH u_c INTO u,d;
if done=1 then
leave c;
ELSE
IF d=‘d1‘ THEN
INSERT INTO pr_user_tmp VALUES(u,d);
END if;
end if;

END LOOP c;
CLOSE u_c;

end;
CALL sys_user();
SELECT * from pr_user_tmp;

简单存储过程+游标

标签:

原文地址:http://www.cnblogs.com/nowphp/p/5549124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!