[英]How to implement Roll Back Transaction and Error Message in MYSQL
I Have sample procedure.我有示例程序。
CREATE PROCEDURE `sample_procedure `
(
IN IDIn bigint(20),
IN MainIDIn bigint(20),
IN NameIn varchar(20)
)
READS SQL DATA
DETERMINISTIC
BEGIN
INSERT INTO tbl_DEPT
(
ID,
Name)
Select 1,'Mohan';
IF NOT EXISTS (SELECT ID FROM tbl_emp te WHERE te.ID = IDIn) THEN
INSERT INTO tbl_emp
(
MainID,
Name)
VALUES (MainIDIn,
NameIn);
ELSE
IF EXISTS (SELECT ID FROM tbl_emp te WHERE te.ID = IDIn) THEN
UPDATE tbl_emp
set
MainID =MainIDIn,
name = NameIn
WHERE te.ID= IDIn;
END IF;
END IF;
END
Call sample_procedure(1,2,Sampl123)调用 sample_procedure(1,2,Sampl123)
I'm just sending some irrelevant Data into the Procedure so that procedure gets failed.我只是将一些不相关的数据发送到程序中,以便程序失败。 But how we need to implement roll back means it should come to the starting state with out Inserting Records into the tbl_DEPT also.
但是我们需要如何实现回滚意味着它应该从 state 开始,而不是将记录也插入到 tbl_DEPT 中。
IN T-SQL we will have在 T-SQL 中,我们将拥有
BEGIN
SET NOCOUNT ON
BEGIN TRANSACTION
BEGIN TRY
SET @OUT = "success";
COMMIT TRANSACTION
END TRY
BEGIN CATCH
set @out = 'not success';
ROLLBACK TRANSACTION
END CATCH
END
this kind of TRY CATCH blocks and to capture Error这种 TRY CATCH 块并捕获错误
"ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity"
In the same way in MYSQL I'm looking for TRY CATCH and ROLL BACK Mechanism.以同样的方式在 MYSQL 我正在寻找 TRY CATCH 和 ROLL BACK 机制。
IF Procedure fails it should ROLL BACK and Not to load in any of the table.如果过程失败,它应该回滚并且不加载到任何表中。
Can any one Suggest me in MYSQL.任何人都可以在 MYSQL 中建议我。
Mysql does not use try/catch. Mysql 不使用 try/catch。 If uses handlers, eg the EXIT handler that will terminate the execution of the SP:
如果使用处理程序,例如将终止 SP 执行的 EXIT 处理程序:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
-- other logic
END;
There are other type of handlers as well.还有其他类型的处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.