標籤:
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
mysql用戶端添加預存程序