1. 程式人生 > >mysql 查詢表字段作為其中引數插入到另一表的儲存過程

mysql 查詢表字段作為其中引數插入到另一表的儲存過程

表 m_sp 中一個欄位sp_id,關聯另一個m_sp_k 表,B表是後期加的,導致m_sp_k 表中沒有關聯A錶的歷史資料,如下儲存過程便是解決所述問題。

查詢結果迴圈遍歷,結果值作為新的引數插入新表中。

create procedure proc_tmp()

BEGIN

declare done int default 0;
declare relationId VARCHAR(32); 

declare idCur cursor for select A.sp_id from m_sp as A;

declare continue handler for not FOUND set done = 1;

open idCur; 

/* 迴圈開始 */
REPEAT

fetch idCur into relationId;
if not done THEN
insert into m_sp_k(s_k_id, sp_id, s_k_key, s_k_sec, s_k_time, s_k_sta, s_k_tp)

values(REPLACE (UUID(), '-', ''), relationId, REPLACE (UUID(), '-', ''), REPLACE (UUID(), '-', ''), NOW(), 0,  0);
end if;
until done end repeat;

close idCur;
END