Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Transaction in SQL Server

$
0
0

Hello Guys,

What is the standardized approach for using transaction in SQL Server?

I have a stored procedure that has multiple steps and i want those to be enclosed within transaction.

I googled the approaches and i found out the following types:-

1. This is the first way we can use transactions:-

CREATE PROCEDURE spNewStoredProcedure1

@Parameter1 INT

BEGIN

BEGIN TRANSACTION

/* Step 1 */

IF @@ERROR <> 0
      BEGIN
          RETURN
      END

/* Step 2 */

IF @@ERROR <> 0

BEGIN

RETURN

END

COMMIT TRANSACTION

END

2. This is the second way we can use transactions:-

CREATE PROCEDURE spNewStoredProcedure1

@Parameter1 INT

BEGIN

BEGIN TRANSACTION

BEGIN TRY

/* Step 1 */

/* Step 2 */

COMMIT TRANSACTION

END TRY
    BEGIN CATCH

        /* Rollback the transaction */
        ROLLBACK TRANSACTION

    END CATCH

END

Which is the better way of using the transactions?

Is try and catch native to SQL Server or is it part of CLR integration.

Please tell me why one is better than the other.
Is there a performance benefit?
Please elaborate you answer.

Thanks in advance



Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>