mysql用戶端添加預存程序

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.