Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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 Comments1 Solution826 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
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShahFlag of India image

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

Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers