OVH Cloud OVH Cloud

Recuperer plusieurs valeurs de retour a partir d'une proc stockee

2 réponses
Avatar
Bonjour
soit la Procedure stockee suivante

CREATE PROCEDURE PS_ajout_nouveau_candidat
@nom varchar(35),
@prenom varchar(35),
@email varchar(50),
@login varchar(25),
@password varchar(35),
@cand_id int OUTPUT ,
@resultat_email int OUTPUT ,
@resultat_login int OUTPUT
AS
DECLARE
@nouv_login INT,
@nouv_email Int

BEGIN
-- insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Debut
nom= '+@nom+ ' login=' + @login);

-- recherche de l'existence d'un login

SELECT @nouv_login = count(cand_id)
FROM CANDIDAT
WHERE cand_login = @login;

-- recherche de l'existence d'un email
SELECT @nouv_email = count(cand_id)
FROM CANDIDAT
WHERE cand_email = @email;

insert into debogage(op_desc) values('PS_ajout_nouveau_candidat count
cand_id ='+CONVERT(varchar(50),@nouv_login)+@nom+ ' email : ' + @email);

if (@nouv_login=0 AND @nouv_email = 0)
begin
SET @nouv_login = 0;

insert into debogage(op_desc) values('PS_ajout_nouveau_candidat
login inexistant et email inexistant '+@nom+ ' email : ' + @email);
-- --création d'un nouveau candidat
insert into CANDIDAT (cand_nom,
cand_prenom,
cand_email,
cand_login,
cand_pass)
values (@nom,
@prenom,
@email,
@login,
@password);
SELECT @cand_id = @@Identity FROM CANDIDAT WHERE
cand_login=@login
end
else

begin
if @nouv_email >0
insert into debogage(op_desc) values('PS nouveau candidat
Pas inscription cause : email existant '+@email);
if @nouv_login > 0
insert into debogage(op_desc) values('PS nouveau candidat Pas
inscription cause : login existant '+@login);
end

set @resultat_Email = @nouv_email
set @resultat_Login = @nouv_login
set @cand_id = @nouv_login
insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Fin nom=
'+@nom);
insert into debogage(op_desc) values('PS_ajout_nouveau_candidat fin PS
count cand_id ='+CONVERT(varchar(50),@cand_id));
return @cand_id
return @resultat_Email
return @resultat_Login
END
GO

Cette procedure verifie au prelable qu'il n'existe pas dans la table, un
login ou un email deja existant avant d'ajouter l'enregistrement

Comment recuperer les valeurs de retour :
return @cand_id
return @resultat_Email
return @resultat_Login

dans un script ASP ou ASP.Net...?
Merci pour votre aide

2 réponses

Avatar
hch
votre probleme est un probleme de recuperation de parametres output d'une
stored procedure a patir d'un environnement de dev (ASP asp.net etc..)

pour ceci il faut voir du coté des objets commandes de ADO ou ADO.NET

en effet ces objets sont faits pour executer des procedures stockées SQL et
recuperer ds la collection parameters de l'objet commande les valeurs
d'output ...

Voici un exemple plutot generique qui vous peremttra de vous familiariser
avec le code ADO
*******************************************************
SqlCommand sampleCMD = new SqlCommand ( "SampleProc", myConn );
sampleCMD.CommandType = CommandType.StoredProcedure;

SqlParameter sampParm = sampleCMD.Parameters.Add
( "RETURN_VALUE", SqlDbType.Int );
sampParm.Direction = ParameterDirection.ReturnValue;

sampParm = sampleCMD.Parameters.Add
( "@InputParm", SqlDbType.NVarChar, 12 );
sampParm.Value = "Sample Value";

sampParm = sampleCMD.Parameters.Add
( "@OutputParm", SqlDbType.NVarChar, 28 );
sampParm.Direction = ParameterDirection.Output;

myConn.Open ( );
SqlDataReader sampReader = sampleCMD.ExecuteReader ( );
Console.WriteLine ( "{0}, {1}", sampReader.GetName ( 0 ),
sampReader.GetName ( 1 ) );

while ( sampReader.Read ( ) ) {
Console.WriteLine ( "{0}, {1}", sampReader.GetInt32 ( 0 ),
sampReader.GetString ( 1 ) );
}

sampReader.Close ( );
myConn.Close ( );

Console.WriteLine ( " @OutputParm: {0}",
sampleCMD.Parameters [ "@OutputParm" ].Value );
Console.WriteLine ( "RETURN_VALUE: {0}",
sampleCMD.Parameters [ "RETURN_VALUE" ].Value );
Dim sampleCMD As SqlCommand = New SqlCommand ( "SampleProc", myConn )
sampleCMD.CommandType = CommandType.StoredProcedure

Dim sampParm As SqlParameter = sampleCMD.Parameters.Add _
( "RETURN_VALUE", SqlDbType.Int )
sampParm.Direction = ParameterDirection.ReturnValue

sampParm = sampleCMD.Parameters.Add _
( "@InputParm", SqlDbType.NVarChar, 12 )
sampParm.Value = "Sample Value"

sampParm = sampleCMD.Parameters.Add _
( "@OutputParm", SqlDbType.NVarChar, 28 )
sampParm.Direction = ParameterDirection.Output

myConn.Open ( )
Dim sampReader As SqlDataReader = sampleCMD.ExecuteReader ( )
Console.WriteLine ( "{0}, {1}", sampReader.GetName ( 0 ), _
sampReader.GetName ( 1 ) )

Do While sampReader.Read ( )
Console.WriteLine ( "{0}, {1}", sampReader.GetInt32 ( 0 ), _
sampReader.GetString ( 1 ) )
Loop

sampReader.Close ( )
myConn.Close ( )

Console.WriteLine ( " @OutputParm: {0}", _
sampleCMD.Parameters ( "@OutputParm" ).Value )
Console.WriteLine ( "RETURN_VALUE: {0}", _
sampleCMD.Parameters ( "RETURN_VALUE" ).Value )
************************************************************

hch

"" a écrit :

Bonjour
soit la Procedure stockee suivante

CREATE PROCEDURE PS_ajout_nouveau_candidat
@nom varchar(35),
@prenom varchar(35),
@email varchar(50),
@login varchar(25),
@password varchar(35),
@cand_id int OUTPUT ,
@resultat_email int OUTPUT ,
@resultat_login int OUTPUT
AS
DECLARE
@nouv_login INT,
@nouv_email Int

BEGIN
-- insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Debut
nom= '+@nom+ ' login=' + @login);

-- recherche de l'existence d'un login

SELECT @nouv_login = count(cand_id)
FROM CANDIDAT
WHERE cand_login = @login;

-- recherche de l'existence d'un email
SELECT @nouv_email = count(cand_id)
FROM CANDIDAT
WHERE cand_email = @email;

insert into debogage(op_desc) values('PS_ajout_nouveau_candidat count
cand_id ='+CONVERT(varchar(50),@nouv_login)+@nom+ ' email : ' + @email);

if (@nouv_login=0 AND @nouv_email = 0)
begin
SET @nouv_login = 0;

insert into debogage(op_desc) values('PS_ajout_nouveau_candidat
login inexistant et email inexistant '+@nom+ ' email : ' + @email);
-- --création d'un nouveau candidat
insert into CANDIDAT (cand_nom,
cand_prenom,
cand_email,
cand_login,
cand_pass)
values (@nom,
@prenom,
@email,
@login,
@password);
SELECT @cand_id = @@Identity FROM CANDIDAT WHERE
cand_login=@login
end
else

begin
if @nouv_email >0
insert into debogage(op_desc) values('PS nouveau candidat
Pas inscription cause : email existant '+@email);
if @nouv_login > 0
insert into debogage(op_desc) values('PS nouveau candidat Pas
inscription cause : login existant '+@login);
end

set @resultat_Email = @nouv_email
set @resultat_Login = @nouv_login
set @cand_id = @nouv_login
insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Fin nom > '+@nom);
insert into debogage(op_desc) values('PS_ajout_nouveau_candidat fin PS
count cand_id ='+CONVERT(varchar(50),@cand_id));
return @cand_id
return @resultat_Email
return @resultat_Login
END
GO

Cette procedure verifie au prelable qu'il n'existe pas dans la table, un
login ou un email deja existant avant d'ajouter l'enregistrement

Comment recuperer les valeurs de retour :
return @cand_id
return @resultat_Email
return @resultat_Login

dans un script ASP ou ASP.Net...?
Merci pour votre aide








Avatar
Philippe T [MS]
Bonjour,

Vous pouvez aussi faire un : SELECT @cand_id, @resultat_Email,
@resultat_Login à la fin de votre requête et récupérer cela dans un dataset.

----------------------------------------------------------------------
Philippe TROTIN - Microsoft Service France

"hch" wrote in message
news:
votre probleme est un probleme de recuperation de parametres output d'une
stored procedure a patir d'un environnement de dev (ASP asp.net etc..)

pour ceci il faut voir du coté des objets commandes de ADO ou ADO.NET

en effet ces objets sont faits pour executer des procedures stockées SQL


et
recuperer ds la collection parameters de l'objet commande les valeurs
d'output ...

Voici un exemple plutot generique qui vous peremttra de vous familiariser
avec le code ADO
*******************************************************
SqlCommand sampleCMD = new SqlCommand ( "SampleProc", myConn );
sampleCMD.CommandType = CommandType.StoredProcedure;

SqlParameter sampParm = sampleCMD.Parameters.Add
( "RETURN_VALUE", SqlDbType.Int );
sampParm.Direction = ParameterDirection.ReturnValue;

sampParm = sampleCMD.Parameters.Add
( "@InputParm", SqlDbType.NVarChar, 12 );
sampParm.Value = "Sample Value";

sampParm = sampleCMD.Parameters.Add
( "@OutputParm", SqlDbType.NVarChar, 28 );
sampParm.Direction = ParameterDirection.Output;

myConn.Open ( );
SqlDataReader sampReader = sampleCMD.ExecuteReader ( );
Console.WriteLine ( "{0}, {1}", sampReader.GetName ( 0 ),
sampReader.GetName ( 1 ) );

while ( sampReader.Read ( ) ) {
Console.WriteLine ( "{0}, {1}", sampReader.GetInt32 ( 0 ),
sampReader.GetString ( 1 ) );
}

sampReader.Close ( );
myConn.Close ( );

Console.WriteLine ( " @OutputParm: {0}",
sampleCMD.Parameters [ "@OutputParm" ].Value );
Console.WriteLine ( "RETURN_VALUE: {0}",
sampleCMD.Parameters [ "RETURN_VALUE" ].Value );
Dim sampleCMD As SqlCommand = New SqlCommand ( "SampleProc", myConn )
sampleCMD.CommandType = CommandType.StoredProcedure

Dim sampParm As SqlParameter = sampleCMD.Parameters.Add _
( "RETURN_VALUE", SqlDbType.Int )
sampParm.Direction = ParameterDirection.ReturnValue

sampParm = sampleCMD.Parameters.Add _
( "@InputParm", SqlDbType.NVarChar, 12 )
sampParm.Value = "Sample Value"

sampParm = sampleCMD.Parameters.Add _
( "@OutputParm", SqlDbType.NVarChar, 28 )
sampParm.Direction = ParameterDirection.Output

myConn.Open ( )
Dim sampReader As SqlDataReader = sampleCMD.ExecuteReader ( )
Console.WriteLine ( "{0}, {1}", sampReader.GetName ( 0 ), _
sampReader.GetName ( 1 ) )

Do While sampReader.Read ( )
Console.WriteLine ( "{0}, {1}", sampReader.GetInt32 ( 0 ), _
sampReader.GetString ( 1 ) )
Loop

sampReader.Close ( )
myConn.Close ( )

Console.WriteLine ( " @OutputParm: {0}", _
sampleCMD.Parameters ( "@OutputParm" ).Value )
Console.WriteLine ( "RETURN_VALUE: {0}", _
sampleCMD.Parameters ( "RETURN_VALUE" ).Value )
************************************************************

hch

"" a écrit :

> Bonjour
> soit la Procedure stockee suivante
>
> CREATE PROCEDURE PS_ajout_nouveau_candidat
> @nom varchar(35),
> @prenom varchar(35),
> @email varchar(50),
> @login varchar(25),
> @password varchar(35),
> @cand_id int OUTPUT ,
> @resultat_email int OUTPUT ,
> @resultat_login int OUTPUT
> AS
> DECLARE
> @nouv_login INT,
> @nouv_email Int
>
> BEGIN
> -- insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Debut
> nom= '+@nom+ ' login=' + @login);
>
> -- recherche de l'existence d'un login
>
> SELECT @nouv_login = count(cand_id)
> FROM CANDIDAT
> WHERE cand_login = @login;
>
> -- recherche de l'existence d'un email
> SELECT @nouv_email = count(cand_id)
> FROM CANDIDAT
> WHERE cand_email = @email;
>
> insert into debogage(op_desc) values('PS_ajout_nouveau_candidat count
> cand_id ='+CONVERT(varchar(50),@nouv_login)+@nom+ ' email : ' + @email);
>
> if (@nouv_login=0 AND @nouv_email = 0)
> begin
> SET @nouv_login = 0;
>
> insert into debogage(op_desc) values('PS_ajout_nouveau_candidat
> login inexistant et email inexistant '+@nom+ ' email : ' + @email);
> -- --création d'un nouveau candidat
> insert into CANDIDAT (cand_nom,
> cand_prenom,
> cand_email,
> cand_login,
> cand_pass)
> values (@nom,
> @prenom,
> @email,
> @login,
> @password);
> SELECT @cand_id = @@Identity FROM CANDIDAT WHERE
> cand_login=@login
> end
> else
>
> begin
> if @nouv_email >0
> insert into debogage(op_desc) values('PS nouveau


candidat
> Pas inscription cause : email existant '+@email);
> if @nouv_login > 0
> insert into debogage(op_desc) values('PS nouveau candidat Pas
> inscription cause : login existant '+@login);
> end
>
> set @resultat_Email = @nouv_email
> set @resultat_Login = @nouv_login
> set @cand_id = @nouv_login
> insert into debogage(op_desc) values('PS_ajout_nouveau_candidat Fin


nom > > '+@nom);
> insert into debogage(op_desc) values('PS_ajout_nouveau_candidat fin PS
> count cand_id ='+CONVERT(varchar(50),@cand_id));
> return @cand_id
> return @resultat_Email
> return @resultat_Login
> END
> GO
>
> Cette procedure verifie au prelable qu'il n'existe pas dans la table,


un
> login ou un email deja existant avant d'ajouter l'enregistrement
>
> Comment recuperer les valeurs de retour :
> return @cand_id
> return @resultat_Email
> return @resultat_Login
>
> dans un script ASP ou ASP.Net...?
> Merci pour votre aide
>
>
>
>
>
>