MySQL 中是否有 SQL Server 的 @@error 的等效项

2021-12-26 00:00:00 error-handling database mysql

我想对生产数据库运行更新查询,作为优秀的小开发人员,我正在努力使其尽可能安全.我正在做以下事情

I want to run an update query against a production database and as good little developer I am trying to make it as safe as possible. I am looking to do the following

BEGIN TRANSACTION
    UPDATE table_x SET col_y = 'some_value'
    .
    .
    .
IF (@@error <> 0)
BEGIN
    ROLLBACK
END
ELSE
BEGIN
    COMMIT
END

以上应该在 SQL Server 中工作,但我需要它来处理 MySQL 数据库.

The above should work in SQL Server but I need this to work against a MySQL database.

抱歉,要执行的语句超过 1 个.是的,我知道不需要在事务中包装单个查询.

Sorry, there is more than 1 statement to execute. Yes I am aware of not needing to wrap a single query in a transaction.

推荐答案

我不认为这是必要的,因为有隐式提交/回滚的概念.

I don't think this is necessary as there is the concept of implicit commit/rollback.

来自 MySQL 文档:

默认情况下,MySQL 启动会话对于每个新的连接启用自动提交模式,所以 MySQL 会在每个 SQL 语句之后提交,如果该语句没有返回错误.如果一个语句返回一个错误,提交或回滚行为取决于错误.见章节13.6.13,InnoDB 错误处理".

By default, MySQL starts the session for each new connection with autocommit mode enabled, so MySQL does a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error. See Section 13.6.13, "InnoDB Error Handling".

相关文章