Transaction in SQL Server

Tuesday, October 9, 2012

This is one of the sample procedure ::

ALTER procedure [dbo].[insertInto]
(
@a varchar(16)=null
)
as
begin
declare @c int=0,@d int=0
begin tran
select * from aaa                           -- aaa is the table name
print @@TRANCOUNT             -- 1

begin tran
-- set @c=@@TRANCOUNT
 -- print @c
  print @@TRANCOUNT       -- 2    
          insert Into aaa values (@a)   
   
  
  Select @d = @@ERROR
  print @d
       IF (@d <> 0) GOTO PROBLEM
   --    rollback tran
   --else
   --     Commit tran    
commit tran
print @@TRANCOUNT
commit tran
print @@TRANCOUNT

PROBLEM:
if(@d <> 0)
begin
  print 'ERROR !!'
   rollback tran
--commit tran
end
end