mysql - sql server try catch block 的mysql等价物是什么

标签 mysql sql-server stored-procedures

我正在尝试将存储过程从 SQL Server 迁移到 MySQL。我在转换 try catch block 时遇到了一些问题。我知道 DECLARE HANDLER 可以用来代替 MySQL 中的 TRY/CATCH block 。如何使用 DECLARE HANDLER 在 MySQL 存储过程中复制此 CATCH 语句的功能?

BEGIN CATCH
CLOSE transDetails //cursor name
CLOSE transQuoteOptionDetails//another cursor name
DEALLOCATE transDetails 
DEALLOCATE transQuoteOptionDetails
DECLARE @ErrorMessage   varchar(2000),@ErrorSeverity  tinyint,@ErrorState  tinyint
SET @ErrorMessage  = ERROR_MESSAGE()
SET @ErrorSeverity = ERROR_SEVERITY()
SET @ErrorState    = ERROR_STATE()
Delete From MasterReport_Temp where masterReportRecNo=@masterReportRecNo
Update MasterReport set status=3, errorDescription=@ErrorMessage where recNo=@masterReportRecNo                   

 END CATCH

最佳答案

似乎在 mysql 中没有 try catch 等价物

但是有一种方法可以使用 SIGNAL 处理错误

SIGNAL is the way to “return” an error. SIGNAL provides error information to a handler, to an outer portion of the application, or to the client. Also, it provides control over the error's characteristics (error number, SQLSTATE value, message). Without SIGNAL, it is necessary to resort to workarounds such as deliberately referring to a nonexistent table to cause a routine to return an error.

关于mysql - sql server try catch block 的mysql等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409183/

相关文章:

java - Spring 交易未提交

MySql:如何使用动态选择语句创建临时表?

postgresql - 如何从 plpgsql 中的多个表返回多行?

python - python 中的 switch case 程序

php - Google Maps API + SQL 数据库 + 渲染点 PHP

sql-server - 更新命令上的 SQL Server 错误 - "A severe error occurred on the current command"

c# - ASP .NET MVC 3 模型 + 存储过程

mysql - 在 MySQL 上更新多列

Mysql2::错误:表 "Admin_Users"已存在

sql - 为什么 LEFT JOIN 后行数增加