Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How rollback tran in while loop

How rollback tran in while loop

Scheduled Pinned Locked Moved Database
helptutorialannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amit sahu20
    wrote on last edited by
    #1

    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
    END

    here 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

    P 1 Reply Last reply
    0
    • A amit sahu20

      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
      END

      here 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

      P Offline
      P Offline
      Pranay Rana
      wrote on last edited by
      #2

      Use Try Catch block and in catch block write statement rollback tran this may work for you easily

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups