Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Commit & Rollback Insert

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2011-03-15 : 23:08:57
I'd like to add error handling for insert statement of Table1. If it fails ,need to update Table2 columnA to 0 . If it succeeds, need to update Table2 columnA to 1.

How the query looks like ?
Thanks.

sp3010
Starting Member

8 Posts

Posted - 2011-03-16 : 01:13:39
Declare @ErrorCode int
Select @ErrorCode = @@Error
If @ErrorCode = 0
Begin
Insert table1
Select @ErrorCode = @@Error
End
If @ErrorCode = 0
Begin
Insert ?table2 with 0
Select @ErrorCode = @@Error
end
else
Begin
--Another statement
Insert ?table2 with 1
Select @ErrorCode = @@Error
end
else
End

Return @ErrorCode
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-16 : 04:18:52
Also read about error handling article from Erland Sommarskog.

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -