先上一段代碼sql
/* 建立字符串split函數 */ DROP FUNCTION IF EXISTS SPLIT_STR; CREATE FUNCTION SPLIT_STR( x VARCHAR(255), delim VARCHAR(12), pos INT ) RETURNS VARCHAR(255) RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1), delim, ''); /* 建立同步店鋪頭像的存儲過程 */ DROP PROCEDURE IF EXISTS SCHEMA_CHANGE_1; DELIMITER $$ CREATE PROCEDURE SCHEMA_CHANGE_1 () BEGIN declare _merchant_id bigint; declare _shop_icon varchar(1500); DECLARE done INT DEFAULT FALSE; Declare maxPicId bigint; declare cur cursor for ( select merchant_id,concat("https://",wenjianlj,".oss-cn-hangzhou.aliyuncs.com/",wenjianmc) as path from ( select t2.merchant_id,SPLIT_STR(shanghutp,"_",1) as guid,SPLIT_STR(shanghutp,"_",2) as verfiCode from (select distinct merchant_id from ( select a.merchant_id as merchant_id, b.id as picId from mdse_shop as a left join mdse_pic as b on a.shop_icon=b.id) t1 where picId is null) t2 inner join yygl_v_shangjiaxx as shangjia on t2.merchant_id=shangjia.shanghubh) t3 inner join xtgy_wenjianccsy as wenjian on t3.guid=wenjian.guid and t3.verfiCode=wenjian.yanzhengma ); DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 監聽器 set maxPicId = (select id from mdse_pic order by id desc limit 1); open cur; label:LOOP FETCH cur INTO _merchant_id,_shop_icon; select _merchant_id,_shop_icon; set maxPicId = (maxPicId + 1); insert into mdse_pic(id,path) value(maxPicId,_shop_icon); -- 添加圖片記錄 update mdse_shop set shop_icon= maxPicId where merchant_id=_merchant_id; -- 門店關聯圖片記錄主鍵 IF done THEN LEAVE label;END IF; END LOOP label; close cur; END$$ DELIMITER ; CALL SCHEMA_CHANGE_1 ();
一、刪除函數語法函數
drop function 函數名稱; 一般會和if exists聯合使用。drop function if exists 函數名稱;
二、建立函數語法ui
create function 函數名稱( 參數名稱 參數類型, 參數名稱 參數類型, ... ) returns 返回值類型 return { sql 邏輯語句 }
三、刪除存儲過程code
drop procedure 存儲過程名稱; 一般會和if exists聯合使用,drop procedure if exists 存儲過程名稱;
四、指定指令結束符 開始blog
DELIMITER $$
五、從新指定指令結束符爲;圖片
DELIMITER ;
六、建立存儲過程語法字符串
create procedure 存儲過程名稱() begin 存儲過程內容 end 指令結束符
七、聲明存儲過程變量同步
declare 變量名稱 變量類型 [default 默認值];
八、修改變量值it
set 變量名稱=變量值;
九、聲明遊標io
declare 遊標名稱 cursor for (select查詢);
十、聲明遊標監聽器
DECLARE done INT DEFAULT FALSE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 監聽器 若是找不到下一條記錄,則把一個標誌位變動
十一、開啓遊標,關閉遊標
open cur; 操做遊標 close cur;
十二、遍歷遊標
label:LOOP FETCH cur INTO _merchant_id,_shop_icon; IF done THEN LEAVE label;END IF; END LOOP label;
1三、從遊標獲取一行記錄到變量中
FETCH cur INTO _merchant_id,_shop_icon 注意一點 遊標中的列名稱不能和變量名重複,負責不會對變量進行賦值
1四、調用存儲過程
CALL 存儲過程名稱 ();