在SQL Server作业的每个步骤中,我们都有DML操作。为了确保更新/插入的情况下将回滚不顺心的事,我已经包裹的数据修改的每一步中TRY CATCH
和TRANSACTION
块:
BEGIN TRY
BEGIN TRANSACTION
[[INSERT/update statements]] ...
IF @@TRANCOUNT > 0
BEGIN
COMMIT TRANSACTION
PRINT 'Successful.'
END
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
PRINT 'Unsuccessful.'
END
END CATCH
是否确保在发生错误的情况下回滚数据操作?还是应该考虑其他因素?
会有更好的方法(使用配置等)吗?
谢谢。