Use of Exception Handling in SQL Server.
How to use Exception Handling in SQl Server?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Nov 3, 2012, 4:12 AM EDT
For Example we have two table tbl_userinfo and tbl_login. We want that when a user register then create his/her login means we want entry in both table.
Suppose user register and values inserted in tbl_userinfo but not inserted in tbl_login in that case we want rollback values from tbl_userinfo also.So
begin try
begin tran userreg
declare @userid int
insert into tbl_userinfo(name, email)
values('sandeep','[email protected]')
select @userid=max(user_id) from tbl_userinfo
insert into tbl_login(login_id, user_id, user_name, password)
values(2,@userid,'singh','singh')
end try
begin catch
rollback tran userreg
end catch
http://geekswithblogs.net/madhawa/archive/2008/08/25/124661.aspx
Rajeev KumarRank #1,334 · 1.3K reputationPosted Jul 10, 2023, 3:46 AM EDT
Here We can understand to use TRY...CATCH block to handle Exception handling in SQL Server
it is simple --
begin try
end try
begin catch
print @@error
end catch