原來mysql預存程序是沒有數組概念的,網上資料也少。drop procedure if exists Pro_DeleteLog; -- 存在即刪除create procedure Pro_DeleteLog( Qualification varchar(100)) -- 定義參數begin declare i int ; declare Start1 int; declare Length int ; declare TotalLenght int ; declare filed varchar(100) ; declare sqlStr varchar(2000) ; declare stmtNovelSearch varchar(2000) ; set i=1; set Start1=1; set Length=0; set TotalLenght=length(Qualification); -- 計算輸入參數長度 select TotalLenght; while i <=TotalLenght do -- i=1 開始 -- select SUBSTRING(Qualification,i,1); if(SUBSTRING(Qualification,i,1)=',') -- 注意 mysql 的substring函數截取字串是從1開始的,而不是0 與java JavaScript不同。-- 從第i位開始截取一個字元,看是否等於, then set filed=SUBSTRING(Qualification,Start1,Length);-- 從start1開始截取length長度的字元 select filed; set @sqlStr = CONCAT('insert into end_results(accessid,filedName,errinfo)select * from (select w1.accessid ,\'',filed ,'\' as filedName,',filed,' from wdd_audit w1 ,wdd_dbaccessinfo w2 where w1.accessid=w2.accessid )a where not exists(select e.',filed ,' from exp_results e where e.',filed,'=a.',filed,' or(e.',filed,' is null and a.',filed,' is null) );'); -- 拼裝sql語句select @sqlStr; PREPARE stmtNovelSearch FROM @sqlStr; -- prepare 必須採用這種傳參方式 execute stmtNovelSearch; deallocate prepare stmtNovelSearch; set Start1=i+1; set Length=0; else set Length=Length+1; -- 如果不是, 則截取的長度加1 end if; set i=i+1; end while;end-- 調用預存程序 欄位名以,分隔 注意最後 , 結尾call Pro_DeleteLog('payload,backresult,dataset,effectrow,clientuser,loginuser,clientprg,')