mysql 存储过程

本文回顾了MySQL存储过程,适合新手学习。通过具体的代码示例,详细介绍了如何创建和使用存储过程。
摘要由CSDN通过智能技术生成

好久没有写过存储过程了,今天又把mysql的存储过程熟悉了一次,对于新手来说这是一个很好的demo.

具体代码如下:

DROP PROCEDURE IF EXISTS `proc_demo`$$

CREATE DEFINER=`root`@`%` PROCEDURE `proc_demo`()
BEGIN
	DECLARE done INT;
	DECLARE pid INT;
	DECLARE cur CURSOR FOR SELECT DISTINCT placement_id FROM t_placement_type_rel;	
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done =1;
	OPEN cur;
	read_loop:LOOP
	FETCH cur INTO pid;
	IF done THEN
	 LEAVE read_loop;
	END IF;
	   INSERT INTO t_placement_type_rel(placement_id,type_id,STATUS,create_time) VALUES(pid,6,1,SYSDATE());
	END LOOP;
    END$$

创建数据库脚本代码如下:

create table `t_placement_type_rel` (
	`id` int (11),
	`placement_id` int (11),
	`type_id` int (11),
	`status` int (1),
	`create_time` timestamp 
); 

插入测试数据:

insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('1','1','1','0','2015-06-09 09:43:13');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('2','1','2','0','2015-06-09 09:43:13');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('3','1','3','1','2015-06-09 09:43:13');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('4','1','4','1','2015-06-09 09:43:13');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('5','2','1','1','2015-06-09 09:53:48');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('6','2','2','0','2015-06-09 09:53:48');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('7','2','3','0','2015-06-09 09:53:48');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('8','2','4','1','2015-06-09 09:53:48');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('9','3','1','0','2015-06-09 10:12:45');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('10','3','2','0','2015-06-09 10:12:45');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('11','3','3','0','2015-06-09 10:12:45');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('12','3','4','1','2015-06-09 10:12:45');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('13','4','1','0','2015-06-09 10:19:29');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('14','4','2','1','2015-06-09 10:19:29');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('15','4','3','0','2015-06-09 10:19:29');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('16','4','4','1','2015-06-09 10:19:29');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('17','5','1','0','2015-06-09 14:39:12');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('18','5','2','0','2015-06-09 14:39:12');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('19','5','3','1','2015-06-09 14:39:12');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, `create_time`) values('20','5','4','1','2015-06-09 14:39:12');
insert into `t_placement_type_rel` (`id`, `placement_id`, `type_id`, `status`, 	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值