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:-
CREATEPROCEDURE spNewStoredProcedure1
@Parameter1 INT
BEGIN
BEGINTRANSACTION
/* Step 1 */
IF@@ERROR <>0
BEGIN
RETURN
END/* Step 2 */
IF@@ERROR <>0
BEGIN
RETURN
END
COMMITTRANSACTION
END
2. This is the second way we can use transactions:-
CREATEPROCEDURE spNewStoredProcedure1
@Parameter1 INT
BEGIN
BEGINTRANSACTION
BEGIN TRY
/* Step 1 */
/* Step 2 */
COMMITTRANSACTION
END TRY
BEGIN CATCH
/* Rollback the transaction */
ROLLBACKTRANSACTION
END CATCHEND
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.