Solved

Cursor loop problem maybe to do with @@FETCH_STATUS

Posted on 2000-05-15
3
630 Views
Last Modified: 2008-01-16
Hi Guys

Perhaps one of you could tell me why I seem to have an endless loop in this code. I've been sat staring at it for ages but can't see anything wrong.

WHILE @@FETCH_STATUS=0

                    --Retrieve data from PlannerHistory
                    IF EXISTS (SELECT * FROM PlannerHistory WHERE PubNo = @curPubNoHistory AND Period = @pPeriod)
                   
                       BEGIN

                          SELECT @PubName = PubName, @SalesTarget = SalesTarget, @LastYearSales = LastYearSales, @SalesGrowth = SalesGrowth,
                             @BudgetCoreWagePercent = BudgetCoreWagePercent, @CoreWagePercentTarget = CoreWagePercentTarget,
                             @BudgetAmendment = BudgetAmendment, @AmendedCoreWagePercentTarget = AmendedCoreWagePercentTarget,
                             @AveragePayRate = AveragePayRate,  @TotalFixedCosts = TotalFixedCosts, @CalculatedHours = CalculatedHours,
                             @TargetHours = TargetHours, @HoursVariance = HoursVariance, @ForecastCoreWagePercent = ForecastCoreWagePercent,
                             @TotalHours = TotalHours, @IdealManagement = IdealManagement, @CurrentManagers = CurrentManagers, @ManagementGap = ManagementGap
                             FROM PlannerHistory
                             WHERE PubNo = @curPubNoHistory AND Period = @pPeriod

                          --Put the data into a temporary table
                          INSERT #Planner
                             SELECT @curPubNoHistory, @PubName, @SalesTarget, @LastYearSales, @SalesGrowth, @BudgetCoreWagePercent, @CoreWagePercentTarget,
                                @BudgetAmendment, @AmendedCoreWagePercentTarget, @AveragePayRate,  @TotalFixedCosts, @CalculatedHours,
                                @TargetHours, @HoursVariance, @ForecastCoreWagePercent, @TotalHours, @IdealManagement, @CurrentManagers, @ManagementGap
                       END
                               
                --Get the next pub in the cursor
                FETCH PubNumbers
                   INTO @curPubNoHistory
0
Comment
Question by:jgianni
3 Comments
 
LVL 1

Accepted Solution

by:
jboyd earned 50 total points
ID: 2811093
Your fetch next is outside the while loop. Add a BEGIN right after the While statament and an END right after the fetch into statement.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2811110
Confirmation!

I assume however that you also have
-Get the first pub in the cursor
FETCH PubNumbers
INTO @curPubNoHistory
before your WHILE statement
0
 

Author Comment

by:jgianni
ID: 2813718
Thank you very much.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

821 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