mysql客户端添加存储过程

DROP PROCEDURE IF EXISTS add_costItem;
DELIMITER $$

CREATE
PROCEDURE `cloud_org`.`add_costItem`()
BEGIN
#定义 变量
DECLARE costTypeId BIGINT;
DECLARE costTypeCode VARCHAR(32);
DECLARE acctTypeId INT;
DECLARE bizZhyCode VARCHAR(40);
DECLARE stationCode VARCHAR(40);

DECLARE str VARCHAR(300);
DECLARE numIndex INT;

DECLARE s INT DEFAULT 0;

DECLARE cursor_name CURSOR FOR SELECT cost_type_id,cost_type_code,acct_type_id,biz_zhy_code,server_station_code FROM sys_cost_item WHERE server_station_code NOT IN (SELECT server_station_code FROM sys_cost_item WHERE cost_item_name = '无' AND server_station_code!='' ) GROUP BY cost_type_id;

#设置一个终止标记


DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;


SET str = "--";
#打开游标


OPEN cursor_name;

#获取游标当前指针的记录,读取一行数据并传给变量


FETCH cursor_name INTO costTypeId,costTypeCode,acctTypeId,bizZhyCode,stationCode;
#开始循环,判断是否游标已经到达了最后作为循环条件


WHILE s <> 1 DO
SET str = CONCAT(str,numIndex);

INSERT INTO `sys_cost_item`(`cost_item_name`,`cost_item_desc`,`cost_type_id`,`cost_type_code`,`status`,`is_preset`,`acct_type_id`,`price`,`unit`,`biz_zhy_code`,`server_station_code`,`create_time`,`create_user_id`,`last_update_user_id`,`update_time`,`last_update_no`,`extra_param`,`modify_rule`) VALUES
('无',NULL,costTypeId,costTypeCode,1,2,acctTypeId,'0.00',1,bizZhyCode,stationCode,NULL,NULL,NULL,'2016-08-24 16:02:53',0,'',1);
#读取下一行的数据
FETCH cursor_name INTO costTypeId,costTypeCode,acctTypeId,bizZhyCode,stationCode;

END WHILE;

#关闭游标


CLOSE cursor_name;

SELECT str;
#语句执行结束
END$$

DELIMITER ;

 创建完之后,调用方法 call add_costItem();

以后每次都会执行存储过程的结果,可以现在编辑器中点击创建存储过程,然后把内容复制到begin到end之间

说明:由于规范有的公司可能禁用存储过程,可以提前写个简单的存储过程试试

参考:http://shitou521.iteye.com/blog/1069027

posted @ 2016-08-24 13:51  黄昏的麦田  阅读(1341)  评论(0编辑  收藏  举报