SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Transaction count after EXECUTE indicates a mismat
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

marclas
Starting Member

Camaroon
16 Posts

Posted - 08/09/2012 :  12:00:48  Show Profile  Reply with Quote
Hi,
I have wrote the next procedure
[code="sql"]
USE [RECLACSR-DB]
GO
/****** Object: StoredProcedure [dbo].[InfoPrComplement] Script Date: 08/09/2012 15:00:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[InfoPrComplement]
-- Add the parameters for the stored procedure here
@node nchar (10),
@resultat nvarchar (100) ='Paiement Complet' output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;

begin transaction
----Verifier la valeur du titre
begin Try
Declare @Valeurtitre int;

set @Valeurtitre = (select PAR_CATEGORIE.CAT_MONTANT
from CSR_DOSSIER,PAR_CATEGORIE
where CSR_DOSSIER.CAT_CODE = PAR_CATEGORIE.CAT_CODE
and CSR_DOSSIER.DOS_NODE = @node);

Declare @Montantverser int;

set @Montantverser = (Select SUM (dbo.CSR_COMPLEMENT.CMP_MTVERSE)
From dbo.CSR_COMPLEMENT WHERE dbo .CSR_COMPLEMENT.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID
FROM dbo.CSR_DOSSIER
WHERE DOS_NODE =@node ));
if (@Montantverser = @Valeurtitre )
begin
-----------resultat "Paiement complet"
set @resultat = 'Paiement complet';
end
else
begin
SELECT dbo.CSR_DOSSIER.DOS_NOM +' '+ ISNULL ( dbo .CSR_DOSSIER.DOS_PRENOM, ' ') as Nom,
CSR_DOSSIER.PID_CODE +' - '+PAR_PIDENT.PID_LIB AS Poste , CSR_DOSSIER.TIT_CODE +' - '+ PAR_TITRE.TIT_LIB AS Titre ,
CSR_DOSSIER.CAT_CODE, dbo.PAR_CATEGORIE.CAT_MONTANT,
(Select SUM (dbo.CSR_COMPLEMENT.CMP_MTVERSE)
From dbo.CSR_COMPLEMENT WHERE dbo .CSR_COMPLEMENT.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID
FROM dbo.CSR_DOSSIER
WHERE DOS_NODE =@node )) as MontantVerse
FROM dbo.CSR_DOSSIER ,dbo.PAR_CATEGORIE ,dbo.PAR_PIDENT, PAR_TITRE
WHERE dbo .CSR_DOSSIER.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID
FROM dbo.CSR_DOSSIER WHERE DOS_NODE =@node )
AND CSR_DOSSIER.CAT_CODE = PAR_CATEGORIE .CAT_CODE
AND CSR_DOSSIER.PID_CODE = PAR_PIDENT.PID_CODE
AND CSR_DOSSIER.TIT_CODE = PAR_TITRE.TIT_CODE;
set @resultat ='Paiement Partiel';
end
return @resultat
end try
begin catch
SELECT ERROR_PROCEDURE() AS ErrorProcedure ,ERROR_LINE() AS ErrorLine,ERROR_MESSAGE() AS ErrorMessage;

-----------IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
end catch

END
[/code]
it's compile well, but when i execute , althought i have teh result, ialso got thesse error
quote:
Msg 266, Niveau 16, État 2, Procédure InfoPrComplement, Ligne 0
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 7, current count = 8.

(1 ligne(s) affectée(s))

(1 ligne(s) affectée(s))
Msg 3998, Niveau 16, État 1, Ligne 1
Uncommittable transaction is detected at the end of the batch. The transaction is rolled back.

And I don't know why.

My return value also gives this message
ErrorProcedure ErrorLine ErrorMessage
InfoPrComplement 53 Conversion failed when converting the nvarchar value 'Paiement Partiel' to data type int.

@resultat
NULL

Return Value
-6

thanks for your help

Marclas

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/09/2012 :  12:05:57  Show Profile  Reply with Quote
i cant see begin tran. where exactly are you starting transaction?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3832 Posts

Posted - 08/09/2012 :  13:09:19  Show Profile  Reply with Quote
There is no commit.. try this:
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;

----Verifier la valeur du titre
begin Try

	begin transaction

	Declare @Valeurtitre int;

	set @Valeurtitre = (select PAR_CATEGORIE.CAT_MONTANT 
	from CSR_DOSSIER,PAR_CATEGORIE 
	where CSR_DOSSIER.CAT_CODE = PAR_CATEGORIE.CAT_CODE 
	and CSR_DOSSIER.DOS_NODE = @node);

	Declare @Montantverser int;

	set @Montantverser = (Select SUM (dbo.CSR_COMPLEMENT.CMP_MTVERSE) 
	From dbo.CSR_COMPLEMENT WHERE dbo .CSR_COMPLEMENT.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID 
	FROM dbo.CSR_DOSSIER 
	WHERE DOS_NODE =@node ));

	if (@Montantverser = @Valeurtitre )
	begin
		-----------resultat "Paiement complet"
		set @resultat = 'Paiement complet'; 
	end
	else
	begin
		SELECT dbo.CSR_DOSSIER.DOS_NOM +' '+ ISNULL ( dbo .CSR_DOSSIER.DOS_PRENOM, ' ') as Nom, 
		CSR_DOSSIER.PID_CODE +' - '+PAR_PIDENT.PID_LIB AS Poste , CSR_DOSSIER.TIT_CODE +' - '+ PAR_TITRE.TIT_LIB AS Titre ,
		CSR_DOSSIER.CAT_CODE, dbo.PAR_CATEGORIE.CAT_MONTANT,
		(Select SUM (dbo.CSR_COMPLEMENT.CMP_MTVERSE) 
		From dbo.CSR_COMPLEMENT WHERE dbo .CSR_COMPLEMENT.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID 
		FROM dbo.CSR_DOSSIER 
		WHERE DOS_NODE =@node )) as MontantVerse 
		FROM dbo.CSR_DOSSIER ,dbo.PAR_CATEGORIE ,dbo.PAR_PIDENT, PAR_TITRE 
		WHERE dbo .CSR_DOSSIER.DOS_ID =(SELECT dbo.CSR_DOSSIER.DOS_ID 
		FROM dbo.CSR_DOSSIER WHERE DOS_NODE =@node )
		AND CSR_DOSSIER.CAT_CODE = PAR_CATEGORIE .CAT_CODE 
		AND CSR_DOSSIER.PID_CODE = PAR_PIDENT.PID_CODE 
		AND CSR_DOSSIER.TIT_CODE = PAR_TITRE.TIT_CODE; 
		set @resultat ='Paiement Partiel'; 
	end
	
	commit transaction

	return @resultat 
end try
begin catch
IF @@TRANCOUNT > 0 
	ROLLBACK TRANSACTION;

SELECT ERROR_PROCEDURE() AS ErrorProcedure ,ERROR_LINE() AS ErrorLine,ERROR_MESSAGE() AS ErrorMessage;
end catch

END
Go to Top of Page

marclas
Starting Member

Camaroon
16 Posts

Posted - 08/10/2012 :  03:33:03  Show Profile  Reply with Quote
Merci!!
thanks!

Marclas
Go to Top of Page

marclas
Starting Member

Camaroon
16 Posts

Posted - 08/10/2012 :  05:08:28  Show Profile  Reply with Quote
thanks a lot.

is it possible for procedure to have 02 output parameters?

how can i do it? and use them

Marclas
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 08/10/2012 :  05:42:24  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
you can have any number of OUTPUT parameters.

The RETURN parameter should only be used for error levels.

Here's an example:

USE tempdb
GO

IF OBJECT_ID('foo') IS NOT NULL DROP PROCEDURE foo
GO

CREATE PROCEDURE foo @bar INT OUTPUT, @woo INT OUTPUT
AS BEGIN
	SET @bar = 3
	SET @woo = 5
	RETURN 55676
END
GO

DECLARE
	  @a INT
	, @b INT
	, @c INT

SELECT @a,@b,@c

-- Note that @c is not passed as OUTPUT
EXEC @a = dbo.foo @bar = @b OUTPUT, @woo = @c 

SELECT @a,@b,@c


You'll see that the values of @a and @b are set by the proc... @c is not because we didn't pass it with the OUTPUT clause to the sp.

Setting @a this way (the return value of the sp) should only be used to check for an error.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 08/10/2012 :  10:53:45  Show Profile  Reply with Quote
http://www.sqlteam.com/article/stored-procedures-returning-data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000