How rollback tran in while loop
-
ALTER procedure Villas_proc_Insert_EnquiryBookingDetail
(@BId int ,@VId int,@FromDate datetime,@Todate datetime)
As
BEGIN TRAN AAA123
WHILE (@FromDate < @Todate)
BEGIN
insert into Villas_EnquiryBookingDetail(BId,VId,BookedDate)
values(@BId,@VId,@FromDate)
SET @FromDate = dateadd(day,1,@FromDate)
END
update Villas_Enquiry set status= 'Confirm' where BId= @BId
if @@error =0
BEGIN
COMMIT TRAN AAA123
END
ELSE
BEGIN
ROLLBACK TRAN AAA123
ENDhere VId +BookedDate are unique key in Villas_EnquiryBookingDetail table
exe Villas_proc_Insert_EnquiryBookingDetail 5,2,'03/02/2010','03/07/2010';
exe Villas_proc_Insert_EnquiryBookingDetail 5,2,'03/07/2010','03/10/2010'how to roll back whole tran if Violation of UNIQUE KEY constraint hep pend in my procedure
-
ALTER procedure Villas_proc_Insert_EnquiryBookingDetail
(@BId int ,@VId int,@FromDate datetime,@Todate datetime)
As
BEGIN TRAN AAA123
WHILE (@FromDate < @Todate)
BEGIN
insert into Villas_EnquiryBookingDetail(BId,VId,BookedDate)
values(@BId,@VId,@FromDate)
SET @FromDate = dateadd(day,1,@FromDate)
END
update Villas_Enquiry set status= 'Confirm' where BId= @BId
if @@error =0
BEGIN
COMMIT TRAN AAA123
END
ELSE
BEGIN
ROLLBACK TRAN AAA123
ENDhere VId +BookedDate are unique key in Villas_EnquiryBookingDetail table
exe Villas_proc_Insert_EnquiryBookingDetail 5,2,'03/02/2010','03/07/2010';
exe Villas_proc_Insert_EnquiryBookingDetail 5,2,'03/07/2010','03/10/2010'how to roll back whole tran if Violation of UNIQUE KEY constraint hep pend in my procedure
Use Try Catch block and in catch block write statement rollback tran this may work for you easily