如果我只是用我的查询包装:
BEGIN TRANSACTION COMMIT TRANSACTION
如果内部有任何失败,它会自动回滚吗?
从查看其他代码,他们似乎检查错误,如果有错误,然后他们执行GOTO语句,然后调用ROLLBACK TRANSACTION
但这似乎是分配工作,必须在每次插入/更新后检查IF(@@ ERROR <> 0).
我通常在我的存储过程中做这样的事情.它保持了良好和安全,并传递了我遇到的任何错误.
SET XACT_ABORT ON; BEGIN TRY BEGIN TRANSACTION; -- Code goes here COMMIT TRANSACTION; END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; DECLARE @ERROR_SEVERITY INT, @ERROR_STATE INT, @ERROR_NUMBER INT, @ERROR_LINE INT, @ERROR_MESSAGE NVARCHAR(4000); SELECT @ERROR_SEVERITY = ERROR_SEVERITY(), @ERROR_STATE = ERROR_STATE(), @ERROR_NUMBER = ERROR_NUMBER(), @ERROR_LINE = ERROR_LINE(), @ERROR_MESSAGE = ERROR_MESSAGE(); RAISERROR('Msg %d, Line %d, :%s', @ERROR_SEVERITY, @ERROR_STATE, @ERROR_NUMBER, @ERROR_LINE, @ERROR_MESSAGE); END CATCH