Mysql 动态执行多条update语句,带事务的

create procedure  P_CallSql_Tran(IN vi_sql text,in vi_splitchar varchar(20),OUT vo_code VARCHAR(20),out vo_msg text)

BEGIN

DECLARE v_sqltemp text;
DECLARE v_singleSql text;
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
 ROLLBACK;
 SET vo_code='500';
 SET vo_msg='操作失败!';
END;


DECLARE EXIT HANDLER FOR SQLWARNING 
BEGIN
 ROLLBACK;
 SET vo_code='501';
 SET vo_msg='数据格式错误,请检查sql语句的正确性!!!';
END;


SET v_sqltemp=vi_sql;
SET v_singleSql='';
SET AUTOCOMMIT=1;
START TRANSACTION;
WHILE LENGTH(v_sqltemp)>0 DO
  SET v_singleSql=substring_index(v_sqltemp,vi_splitchar,1);
  SET @sqlStr=concat(v_singleSql,';');
PREPARE stmts FROM @sqlStr;
EXECUTE stmts;
  SET v_sqltemp=REPLACE(v_sqltemp,concat(substring_index(v_sqltemp,vi_splitchar,1),vi_splitchar),'');
END WHILE;
 COMMIT;
 SET vo_code='000';
 SET vo_msg='操作成功!';
END
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页