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. BEGIN TRANSACTION AND COMMIT TRANSACTION

BEGIN TRANSACTION AND COMMIT TRANSACTION

Scheduled Pinned Locked Moved Database
helpquestion
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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello All, If I use a try-catch block in my stored proc, and in the catch block write error to table, do I need to use BEGIN TRANSACTION - COMMIT TRANSACTION to get the try-catch block to work? I am using - BEGIN TRY write my code here END TRY BEGIN CATCH write my code here END CATCH But should be - to write errors to a log table. BEGIN TRY BEGIN TRANSACTION write code here COMMIT TRANSACTION END TRY BEGIN CATCH write code here END CATCH

    P 1 Reply Last reply
    0
    • V vanikanc

      Hello All, If I use a try-catch block in my stored proc, and in the catch block write error to table, do I need to use BEGIN TRANSACTION - COMMIT TRANSACTION to get the try-catch block to work? I am using - BEGIN TRY write my code here END TRY BEGIN CATCH write my code here END CATCH But should be - to write errors to a log table. BEGIN TRY BEGIN TRANSACTION write code here COMMIT TRANSACTION END TRY BEGIN CATCH write code here END CATCH

      P Offline
      P Offline
      Phanindra261
      wrote on last edited by
      #2

      no you do not have to(but it is a safer option)..Have a look at the following snippet..-

      USE AdventureWorks2008R2;
      GO

      -- Variable to store ErrorLogID value of the row
      -- inserted in the ErrorLog table by uspLogError
      DECLARE @ErrorLogID INT;

      BEGIN TRY
      BEGIN TRANSACTION;

      -- A FOREIGN KEY constraint exists on this table. This
      -- statement will generate a constraint violation error.
      DELETE FROM Production.Product
          WHERE ProductID = 980;
      
      -- If the delete operation succeeds, commit the transaction.
      COMMIT TRANSACTION;
      

      END TRY
      BEGIN CATCH
      -- Call procedure to print error information.
      EXECUTE dbo.uspPrintError;

      -- Roll back any active or uncommittable transactions before
      -- inserting information in the ErrorLog.
      IF XACT\_STATE() <> 0
      BEGIN
          ROLLBACK TRANSACTION;
      END
      
      EXECUTE dbo.uspLogError @ErrorLogID = @ErrorLogID OUTPUT;
      

      END CATCH;

      -- Retrieve logged error information.
      SELECT * FROM dbo.ErrorLog WHERE ErrorLogID = @ErrorLogID;
      GO

      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