mysql 存储过程 参数 没有数组的处理方式
原来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,')