Solved

Cursor loop problem maybe to do with @@FETCH_STATUS

Posted on 2000-05-15
3
606 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 142

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

13 Experts available now in Live!

Get 1:1 Help Now