[ASE 12.5] cursor dynamique
Bonjour,
après des recherches infructueuses, je sollicite votre aide.
Est-il possible, et si oui comment, de créer un CURSOR dynamiquement ?
Le select dynamique fonctionne sans problème :
Code:
1 2 3 4 5 6 7 8 9 10 11
| CREATE Procedure DYNAPROC
@NUM_HISTO tinyint,
@MAX int
AS
BEGIN
declare @REQUETE char( 1000 )
select @REQUETE = "sp_spaceused HIST_0" + convert( varchar, @NUM_HISTO )
print @REQUETE
exec (@REQUETE)
end |
De même un cursor "normal" en dur fonctionne bien sur sans problème :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| declare CUR_HISTO cursor for select NUM, TP, ST, MONTANT from HIST_02 for read only
open CUR_HISTO
fetch CUR_HISTO into @NUM, @TP, @ST, @MONTANT
select @RET = @@sqlstatus
while( @RET = 0 )
begin
if( @MONTANT >= @MAX )
begin
print "ok"
end
fetch CUR_HISTO into @NUM, @TP, @ST, @MONTANT
select @RET = @@sqlstatus
end |
Par contre, là, ça plante à l'exécution :
Code:
1 2 3 4 5 6
| select @REQUETE = 'declare CUR_HISTO cursor for select NUM, TP, ST, MONTANT from HIST_02 for read only'
exec (@REQUETE)
open CUR_HISTO
fetch CUR_HISTO into @NUM, @TP, @ST, @MONTANT
select @RET = @@sqlstatus
... |
avec l'erreur suivante :
Code:
1 2
| The cursor 'CUR_HISTO' can not be used as it could not be found. It is possible that either it was not declared or it is not
available in the current context. |
J'ai essayé pleins de techniques différentes, mais à chaque fois la même erreur. D'après ce que je comprend, le curseur serait construit dans un processus différent, et après le "exec", le curseur n'existe plus. Pouvez-vous m'aider ?
Merci.