-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to create a cursor to a procedure inside another?
PostPosted: Tue Mar 07, 2006 9:48 am 
Newbie

Joined: Mon Jul 18, 2005 12:51 pm
Posts: 6
Location: São Paulo
Hello folks, does anyone here know, how to create a cursor to a procedure from a function or procedure in MSSQL? Seems very simple, but i can't realise where is my falt, can you help me? Here's my code:

create function dbo.compararPeriodos(
@TABELA_ varchar( 40 ),
@CAMPO_ varchar( 40 ),
@CA_MODULO_ integer,
@CA_USUARIO_ varchar( 20 ),
@CN_ANO_REF_ integer,
@CN_MES_REF_ integer )
returns char( 1 )
as
begin
declare @cmd varchar( 4000 )
declare @status char( 1 )
declare @total numeric( 17, 2 )

declare cursorPesquisa CURSOR
for exec getTotalTabela
@TABELA_,
@CAMPO_,
@CA_MODULO_,
@CA_USUARIO_,
@CN_ANO_REF_,
@CN_MES_REF_

open cursorPesquisa

fetch from cursorPesquisa into
@total

CLOSE cursorPesquisa
DEALLOCATE cursorPesquisa

declare cursorCmp CURSOR
for select case
when @TOTAL >= @TOTAL - FAIXA_TOLERANCIA then 'o'
when @TOTAL >= @TOTAL - FAIXA_TOLERANCIA and @TOTAL <= @TOTAL - FAIXA_TOLERANCIA then 'a'
else 'p'
end
from SIE_MODULOS
where CA_MODULO = @CA_MODULO_

open cursorCmp

fetch from cursorCmp
into @status

CLOSE cursorCmp
DEALLOCATE cursorCmp

return @status
end


Regards,
LottaLava


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 7:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You haven't said what the problem is. That sort of thing helps when trying to diagnose problems.

First thing I'd point out is that your cursors aren't using "fetch next from". I know that "fetch from" is legal syntax, but don't you have to have already moved onto a cursor row? You have to fetch next before you can fetch. fetch from just fetches the current row, and is usually not useful.

Neither of your cursors are iterating: seems very strange to use cursors to access the out params of a stored procedure or simple select. Why not just use the variables directly? If you're forgetting to iterate, the syntax is:
Code:
fetch next from <cursor>
into <variables>

while @@FETCH_STATUS = 0
begin
  <do stuff>

  fetch next from <cursor>
  into <variables>
end
The rest of what you've got looks right to me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.