troubleshooting Question

COMMIT / ROLLBACK usage WITH EXEC giving error on MS SQL Server

Avatar of Serdar Demirkol
Serdar DemirkolFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL ServerSQL
6 Comments4 Solutions826 ViewsLast Modified:
MS SQL Server query giving a script error when I use an EXEC command (Stored Procedure) just after or before COMMIT / ROLLBACK. It is ok when I take the EXEC command lines out.

Could you help please, is there any work around for this?

Thanks,
USE [AdventureWorks]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_test]
AS
BEGIN
	SET NOCOUNT ON;
	--////////////////////////////////////////////
	Declare @ErrorCode int
Select @ErrorCode = @@Error

	Declare @TransactionCountOnEntry int
If @ErrorCode = 0
Begin
   Select @TransactionCountOnEntry = @@TranCount
   BEGIN TRANSACTION
End

Declare @Temp nchar(10)
SELECT @Temp = 'Insertion'
INSERT INTO dbo.Test(logo) VALUES(@Temp)

--////////////////////////////////////////////////
		IF @@TRANCOUNT > @TransactionCountOnEntry
		BEGIN
		   IF @ErrorCode = 0		  
		  COMMIT TRANSACTION
		  EXEC xp_sendmail @recipients=N'someone@somewhere.com', @Subject='JOb has run successfully'

		   ELSE
		  ROLLBACK TRANSACTION
		  EXEC xp_sendmail @recipients=N'someone@somewhere.com', @Subject='JOb has failed'
		END
--/////////////////////////////////////////
END
GO

Open in new window

ASKER CERTIFIED SOLUTION
RiteshShah

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros