Solved

Cursor in Stored Procedure stuck in loop

Posted on 2013-11-18
2
334 Views
Last Modified: 2013-11-18
Hello,

I have a stored procedure that creates a temporary table (with a call to GetGroupNames).

I then want to create and open a cursor to call [CheckMandatoryField] for each row in the table.

It appears to me that the code below never loops through the table, and appears to set @GroupFieldName to the first row in the table each time.

I tested by adding the select statement: select @GroupFieldName.

Can anyone see what i am doing wrong?


 -- Now validate all the other groups.
  select * into #Groups  
  from dbo.GetGroupnames( @doc_id )
  order by ag_Name

  declare @GroupFieldName nvarchar(100);
  
  declare Group_Cursor cursor for
  select ag_Name from #Groups ;
  
  open Group_Cursor ;
  
  fetch next from Group_Cursor into @GroupFieldName
  while (@@fetch_status = 0) 
  begin 
   select @GroupFieldName ;
   
    -- Validate the group
 --   exec @return_value = [CheckMandatoryField] @doc_id = @doc_id, @ddt_FieldName = @GroupFieldName
   -- if (@return_value = 0) set @Valid = 0 ;
  end ;
  
  close Group_Cursor ;
  deallocate Group_Cursor ;
  drop table #Groups ;

Open in new window

0
Comment
Question by:soozh
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
you need to repeat your "fetch next" statement before line 21, as last statement inside the while loop. See also http://technet.microsoft.com/en-us/library/ms180152%28v=sql.100%29.aspx (under Examples)
 -- Now validate all the other groups.
  select * into #Groups  
  from dbo.GetGroupnames( @doc_id )
  order by ag_Name

  declare @GroupFieldName nvarchar(100);
  
  declare Group_Cursor cursor for
  select ag_Name from #Groups ;
  
  open Group_Cursor ;
  
  fetch next from Group_Cursor into @GroupFieldName
  while (@@fetch_status = 0) 
  begin 
   select @GroupFieldName ;
   
    -- Validate the group
 --   exec @return_value = [CheckMandatoryField] @doc_id = @doc_id, @ddt_FieldName = @GroupFieldName
   -- if (@return_value = 0) set @Valid = 0 ;

    -- added next line (copy of same line before while loop)
    fetch next from Group_Cursor into @GroupFieldName

  end ;
  
  close Group_Cursor ;
  deallocate Group_Cursor ;
  drop table #Groups ;

Open in new window

0
 

Author Closing Comment

by:soozh
Comment Utility
I must be blind or stupid not to have seen that!  Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now