Use of Exception Handling in SQL Server.
How to use Exception Handling in SQl Server?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandeep Singh ShekhawatPosted Nov 3, 2012, 8:12 AM
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 KumarPosted Jul 10, 2023, 7:46 AM
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