Posted in: Programming by Steve on December 1, 2010
Today, my manager asked me how I build my T-SQL scripts when I need to update a bunch of data in SQL Server. I hadn’t really thought much about it, but this is what I normally use.
DECLARE @ErrorCode INT BEGIN TRANSACTION --UPDATE A TABLE OR SOMETHING SELECT @ErrorCode = @@ERROR IF (@ErrorCode <> 0) GOTO PROBLEM --DO SOMETHING ELSE SELECT @ErrorCode = @@ERROR IF (@ErrorCode <> 0) GOTO PROBLEM COMMIT TRANSACTION PROBLEM: IF (@ErrorCode <> 0) BEGIN PRINT 'Error Executing Script: Error Code: ' + @ErrorCode ROLLBACK TRANSACTION END
Not sure how anyone else does it, but that’s how I roll. Any suggestions on how this could be improved?