Home > Database > Mysql Tutorial > Mysql存储过程包含事务,且传入sql数据执行_MySQL

Mysql存储过程包含事务,且传入sql数据执行_MySQL

WBOY
Release: 2016-06-01 13:16:05
Original
1124 people have browsed it

有这样一个需求,要求在mysql存储过程中使用到事务,而且执行的是动态的sql语句

代码如下:

BEGIN	DECLARE in_data TEXT;	  /** 标记是否出错 */    DECLARE errno INT DEFAULT '0';    /** 如果出现sql异常,则将t_error设置为1后继续执行后面的操作 */    	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK;SET errno = 1; END;	START TRANSACTION;  	-- 传入的语句处理过程	PREPARE stmt FROM @in_data;	EXECUTE stmt;	IF (errno =1) THEN          ROLLBACK;   ELSE          COMMIT;  		END IF;  	SELECT errno;END
Copy after login
调用 :
SET @in_data = 'insert into accounts (`userid`,`password`) value (122222222,2),(22222,11)';CALL SYN_Updata(@in_data);
Copy after login
其实这个存储过程只要是用到事务的地方都可以用到,因为执行的条件是动态的。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template