OVH Cloud OVH Cloud

Recupération de RecordSet ADO depuis Proc Stockée SQL Serveur

1 réponse
Avatar
gex
Bonjour,

Je souhaite récupérer un RecordSet depuis une procédure stockée. Sur un
exemple simple, cela fonctionne :

Transac SQL :
ALTER procedure dbo._test_ado ( @Cle as int )
AS
BEGIN
select * from Statut where cle= @cle
END

VB :
Set AdoRs = New ADODB.Recordset
AdoRs.CursorType = adOpenStatic
AdoRs.LockType = adLockReadOnly
AdoRs.Open " { Call dbo._test_ado(1) }", AdoCn ' AdoCn :
Connection Ado valide

i = 0
While Not (AdoRs.EOF) ' Code fonctionne : on a tous les
enregistrements...
i = i + 1
AdoRs.MoveNext
Wend


Maintenant, ma procédure stockée est relativement complexe : appel à
d'autres proc stockées, utilisation de curseurs... puis un select final.

Si j'exécute la proc complexe depuis l'analyseur de requète, j'ai bien mon
jeu d'enregistrements.
Si je l'exécute depuis ADO, il n'y a pas de Record Set....

VB :

' Quelque soit la manière de l'appeler depuis ADO, elle n'a pas de
RS.
' Pourtant si on fait un simple select * from dbo._planning_rs_test_
dans la procédure, ça fonctionne depuis ADO

Set AdoRs = New ADODB.Recordset
AdoRs.CursorType = adOpenStatic
AdoRs.LockType = adLockReadOnly
AdoRs.Open " { call dbo._planning_select( '05/01/2003', 3,
1200, -1, -1, -1) }", AdoCn

i = 0
While Not (AdoRs.EOF) /***** Plantage : Erreur 3704 : objet fermé
i = i + 1
AdoRs.MoveNext
Wend

Transac SQL Tronqué :

/*
Enveloppe de la procédure _planning_ pour l'appel depuis l'application VB.

Voir commentaire de _planning_

*/
ALTER procedure _planning_select ( @DateDansSemaine Datetime,
@NombreSemaines int , @CleEleve int = -1, @CleIntervenant int=-1 ,
@CleGroupe int=-1 , @CleSalle int = -1 )
AS
begin



Create TABLE #_planning_rs_

Entete varchar(255) NULL ,
Date_Lundi datetime NOT NULL,
Destinataire int NOT NULL, -- 1 : Eleve ; 2 : Intervenant ; 3 : Groupe
; 4 : Salle
NoQuartJournee tinyint NOT NULL, -- 0 -> 51 ; 52 pour sauter de page

Type_0 tinyint NOT NULL,
NbQuartIntervention_0 tinyint NULL,
QuartCourant_0 tinyint NULL, -- 0 -> NbQuartIntervention_0 - 1
LibModule_0 varchar(50) NULL,
LibGroupe_0 varchar(50) NULL,
LibIntervenant1_0 varchar(50) NULL,
LibIntervenant2_0 varchar(50) NULL,
LibIntervenant3_0 varchar(50) NULL,
NbIntervenants_0 tinyint NULL,
LibSalle1_0 varchar(50) NULL,
LibSalle2_0 varchar(50) NULL,
LibSalle3_0 varchar(50) NULL,
NbSalles_0 tinyint NULL,

)




Declare @Entete varchar(255)
Declare @Date_Lundi datetime
Declare @Destinataire int
Declare @NoQuartJournee tinyint

Declare @Type_0 tinyint
Declare @NbQuartIntervention_0 tinyint
Declare @QuartCourant_0 tinyint
Declare @LibModule_0 varchar(50)
Declare @LibGroupe_0 varchar(50)
Declare @LibIntervenant1_0 varchar(50)
Declare @LibIntervenant2_0 varchar(50)
Declare @LibIntervenant3_0 varchar(50)
Declare @NbIntervenants_0 tinyint
Declare @LibSalle1_0 varchar(50)
Declare @LibSalle2_0 varchar(50)
Declare @LibSalle3_0 varchar(50)
Declare @NbSalles_0 tinyint



DECLARE @lignes_crystal_cursor cursor
-- Définir les valeurs de paramètre
EXEC [dbo].[_planning_] @DateDansSemaine, @NombreSemaines, @CleEleve,
@CleIntervenant, @CleGroupe, @CleSalle, 1 , @lignes_crystal_cursor OUTPUT

FETCH NEXT FROM @lignes_crystal_cursor INTO
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0 ,
@LibSalle3_0 , @NbSalles_0

WHILE (@@FETCH_STATUS = 0)
BEGIN

insert into #_planning_rs_
--insert into _planning_rs_test_
values
(
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0 ,
@LibSalle3_0 , @NbSalles_0
)

FETCH NEXT FROM @lignes_crystal_cursor INTO
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0 ,
@LibSalle3_0 , @NbSalles_0

END

CLOSE @lignes_crystal_cursor
DEALLOCATE @lignes_crystal_cursor

select * from #_planning_rs_ -- Select final pour la récupération du
recordset


end


Vous avez une idée ?

Merci d'avance.

Gérald Reinhart

1 réponse

Avatar
Med Bouchenafa[MVP]
Regarde la méthode NextRecordset de l'objet recordset

--
Salutations
Med Bouchenafa
TETRASET
75015 Paris
"gex" wrote in message
news:#
Bonjour,

Je souhaite récupérer un RecordSet depuis une procédure stockée. Sur un
exemple simple, cela fonctionne :

Transac SQL :
ALTER procedure dbo._test_ado ( @Cle as int )
AS
BEGIN
select * from Statut where cle= @cle
END

VB :
Set AdoRs = New ADODB.Recordset
AdoRs.CursorType = adOpenStatic
AdoRs.LockType = adLockReadOnly
AdoRs.Open " { Call dbo._test_ado(1) }", AdoCn ' AdoCn :
Connection Ado valide

i = 0
While Not (AdoRs.EOF) ' Code fonctionne : on a tous les
enregistrements...
i = i + 1
AdoRs.MoveNext
Wend


Maintenant, ma procédure stockée est relativement complexe : appel à
d'autres proc stockées, utilisation de curseurs... puis un select final.

Si j'exécute la proc complexe depuis l'analyseur de requète, j'ai bien mon
jeu d'enregistrements.
Si je l'exécute depuis ADO, il n'y a pas de Record Set....

VB :

' Quelque soit la manière de l'appeler depuis ADO, elle n'a pas de
RS.
' Pourtant si on fait un simple select * from


dbo._planning_rs_test_
dans la procédure, ça fonctionne depuis ADO

Set AdoRs = New ADODB.Recordset
AdoRs.CursorType = adOpenStatic
AdoRs.LockType = adLockReadOnly
AdoRs.Open " { call dbo._planning_select( '05/01/2003', 3,
1200, -1, -1, -1) }", AdoCn

i = 0
While Not (AdoRs.EOF) /***** Plantage : Erreur 3704 : objet


fermé
i = i + 1
AdoRs.MoveNext
Wend

Transac SQL Tronqué :

/*
Enveloppe de la procédure _planning_ pour l'appel depuis l'application VB.

Voir commentaire de _planning_

*/
ALTER procedure _planning_select ( @DateDansSemaine Datetime,
@NombreSemaines int , @CleEleve int = -1, @CleIntervenant int=-1 ,
@CleGroupe int=-1 , @CleSalle int = -1 )
AS
begin



Create TABLE #_planning_rs_

Entete varchar(255) NULL ,
Date_Lundi datetime NOT NULL,
Destinataire int NOT NULL, -- 1 : Eleve ; 2 : Intervenant ; 3 :


Groupe
; 4 : Salle
NoQuartJournee tinyint NOT NULL, -- 0 -> 51 ; 52 pour sauter de page

Type_0 tinyint NOT NULL,
NbQuartIntervention_0 tinyint NULL,
QuartCourant_0 tinyint NULL, -- 0 -> NbQuartIntervention_0 - 1
LibModule_0 varchar(50) NULL,
LibGroupe_0 varchar(50) NULL,
LibIntervenant1_0 varchar(50) NULL,
LibIntervenant2_0 varchar(50) NULL,
LibIntervenant3_0 varchar(50) NULL,
NbIntervenants_0 tinyint NULL,
LibSalle1_0 varchar(50) NULL,
LibSalle2_0 varchar(50) NULL,
LibSalle3_0 varchar(50) NULL,
NbSalles_0 tinyint NULL,

)




Declare @Entete varchar(255)
Declare @Date_Lundi datetime
Declare @Destinataire int
Declare @NoQuartJournee tinyint

Declare @Type_0 tinyint
Declare @NbQuartIntervention_0 tinyint
Declare @QuartCourant_0 tinyint
Declare @LibModule_0 varchar(50)
Declare @LibGroupe_0 varchar(50)
Declare @LibIntervenant1_0 varchar(50)
Declare @LibIntervenant2_0 varchar(50)
Declare @LibIntervenant3_0 varchar(50)
Declare @NbIntervenants_0 tinyint
Declare @LibSalle1_0 varchar(50)
Declare @LibSalle2_0 varchar(50)
Declare @LibSalle3_0 varchar(50)
Declare @NbSalles_0 tinyint



DECLARE @lignes_crystal_cursor cursor
-- Définir les valeurs de paramètre
EXEC [dbo].[_planning_] @DateDansSemaine, @NombreSemaines, @CleEleve,
@CleIntervenant, @CleGroupe, @CleSalle, 1 , @lignes_crystal_cursor OUTPUT

FETCH NEXT FROM @lignes_crystal_cursor INTO
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0


,
@LibSalle3_0 , @NbSalles_0

WHILE (@@FETCH_STATUS = 0)
BEGIN

insert into #_planning_rs_
--insert into _planning_rs_test_
values
(
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0


,
@LibSalle3_0 , @NbSalles_0
)

FETCH NEXT FROM @lignes_crystal_cursor INTO
@Entete,
@Date_Lundi ,
@Destinataire ,
@NoQuartJournee ,
@Type_0 , @NbQuartIntervention_0 , @QuartCourant_0 , @LibModule_0
, @LibGroupe_0 , @LibIntervenant1_0 , @LibIntervenant2_0 ,
@LibIntervenant3_0 , @NbIntervenants_0 , @LibSalle1_0 , @LibSalle2_0


,
@LibSalle3_0 , @NbSalles_0

END

CLOSE @lignes_crystal_cursor
DEALLOCATE @lignes_crystal_cursor

select * from #_planning_rs_ -- Select final pour la récupération du
recordset


end


Vous avez une idée ?

Merci d'avance.

Gérald Reinhart