Solved

Cursor in Stored Procedure stuck in loop

Posted on 2013-11-18
2
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39657567
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
ID: 39657676
I must be blind or stupid not to have seen that!  Thanks
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

752 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