Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 While loop only running once and not looping

Author  Topic 

ahouse
Starting Member

27 Posts

Posted - 2008-12-22 : 13:44:12
Hi, I am working on a piece of coding that loops thru each client and processes a file containing their lines of information from the table. Once all clients have been looped thru, the stored proc should finish. I am testing this with Query Analyzer

I am using the field WasProcessed to control this. When the file is initially created, WasProcessed is set to zero for everything. Then as files get pushed (using dts calls) they are set to something other than zero. For testing purposes, I have replaced the dts calls with UPDATE statements so I can see what is happening.

The query analyzer always reports back to me

(1 row(s) affected)


(XX row(s) affected)
(depending on number of rows for that client)

It should loop thru and do this for each client. Not just the first one it comes to.

Any help is appreciated. Thanks everyone!
Andrew



--Declare Variables
Declare @CurrentProgramID int
Declare @CurrentClientID int
Declare @AccountsTilCompletion int
Declare @CurrentReturnName char(15)
Declare @CurrentReturnType int

--Initialize variables to begin looping through locates
Set @CurrentProgramID = (Select Min(ProgramID) From DailyLocates Where DailyLocates.WasProcessed = 0)
Set @CurrentClientID = (Select Min(ClientID) From DailyLocates Where DailyLocates.ProgramID = @CurrentProgramID)
Set @AccountsTilCompletion = (Select Count(*) From DailyLocates Where DailyLocates.WasProcessed = 0)

--Loop until all Located Accounts have been processed

While (@AccountsTilCompletion > 0)
Begin
Set @CurrentProgramID = (Select Min(ProgramID) From DailyLocates Where DailyLocates.WasProcessed = 0);

Set @CurrentReturnName = (Select distinct(ReturnName) From DailyLocates Where DailyLocates.ProgramID = @CurrentProgramID);

Set @CurrentReturnType = (Select Min(ReturnTypeID) From DailyLocates Where DailyLocates.ProgramID = @CurrentProgramID);

--Invoke DTS packages to run based on ReturnFile Type
Begin Select @CurrentReturnType
If @CurrentReturnType = 0 GoTo CatchTheException;
If @CurrentReturnType = 1 GoTo CreateXLSReturn;
If @CurrentReturnType = 2 GoTo CreateTextReturn;
If @CurrentReturnType = 3 GoTo CreatePDFReturn;
If @CurrentReturnType = 4 GoTo CreateCSVReturn;
If @CurrentReturnType = 5 GoTo CreateFaxReturn;
If @CurrentReturnType = 6 GoTo CreateXLSPDFReturn;
End
--Loop until DTS package has completed
--When DTS package completes, DailyLocates.WasProcessed will be set to '1' on all current accounts and this loop will end
While((Select Count(*) From DailyLocates Where DailyLocates.WasProcessed = 0 AND DailyLocates.ProgramID = @CurrentProgramID) > 0)
Begin
WaitFor Delay '00:00:02';
End

Set @CurrentProgramID = (Select Min(ProgramID) From DailyLocates Where DailyLocates.WasProcessed = 0);
Set @AccountsTilCompletion = (Select Count(*) From DailyLocates Where DailyLocates.WasProcessed = 0);
End

CatchTheException:
Update DailyLocates
Set WasProcessed = 2
Where DailyLocates.ProgramID = @CurrentProgramID
Return

CreateXLSReturn:
Update DailyLocates
Set WasProcessed = 3
Where DailyLocates.ProgramID = @CurrentProgramID
Return

CreateTextReturn:
Update DailyLocates
Set WasProcessed = 4
Where DailyLocates.ProgramID = @CurrentProgramID
Return

CreatePDFReturn:
Update DailyLocates
Set WasProcessed = 5
Where DailyLocates.ProgramID = @CurrentProgramID
Return

CreateCSVReturn:
Update DailyLocates
Set WasProcessed = 6
Where DailyLocates.ProgramID = @CurrentProgramID
Return

CreateFaxReturn:

Update DailyLocates
SET WasProcessed = 7
Where DailyLocates.ProgramID = @CurrentProgramID

Return

CreateXLSPDFReturn:
Update DailyLocates
Set WasProcessed = 8
Where DailyLocates.ProgramID = @CurrentProgramID
Return
GO

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-22 : 14:15:04
Have you added PRINT statements to the loop to determine what the values are? You'll need to see the values of @CurrentReturnType and @AccountsTilCompletion in order to debug this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ahouse
Starting Member

27 Posts

Posted - 2008-12-22 : 15:11:28
I added the print statements periodically and found that my procedure never gets past the function call.

Is it legal to use nested Begin and End statements? Are my functions missing a piece allowing them to return to the while loop to finish executing?

--Invoke DTS packages to run based on ReturnFile Type
Begin Select @CurrentReturnType
If @CurrentReturnType = 0 GoTo CatchTheException;
If @CurrentReturnType = 1 GoTo CreateXLSReturn;
If @CurrentReturnType = 2 GoTo CreateTextReturn;
If @CurrentReturnType = 3 GoTo CreatePDFReturn;
If @CurrentReturnType = 4 GoTo CreateCSVReturn;
If @CurrentReturnType = 5 GoTo CreateFaxReturn;
If @CurrentReturnType = 6 GoTo CreateXLSPDFReturn;
End
Print @CurrentReturnType
Print @AccountsTilCompletion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-22 : 15:15:51
I don't understand that piece of code, specifically the first line. Can't you just remove the begin and end statements as they don't seem to make sense there?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-12-22 : 15:32:06
quote:
Originally posted by ahouse

I added the print statements periodically and found that my procedure never gets past the function call.

Is it legal to use nested Begin and End statements? Are my functions missing a piece allowing them to return to the while loop to finish executing?

--Invoke DTS packages to run based on ReturnFile Type
Begin Select @CurrentReturnType
If @CurrentReturnType = 0 GoTo CatchTheException;
If @CurrentReturnType = 1 GoTo CreateXLSReturn;
If @CurrentReturnType = 2 GoTo CreateTextReturn;
If @CurrentReturnType = 3 GoTo CreatePDFReturn;
If @CurrentReturnType = 4 GoTo CreateCSVReturn;
If @CurrentReturnType = 5 GoTo CreateFaxReturn;
If @CurrentReturnType = 6 GoTo CreateXLSPDFReturn;
End
Print @CurrentReturnType
Print @AccountsTilCompletion




Thats not a function that you are using. Those are labels instead. GOTO statement is taking you out of the loop to CatchTheException,CreateXLSReturn etc. You should rework the logic that you are using here.
Go to Top of Page

ahouse
Starting Member

27 Posts

Posted - 2008-12-22 : 16:50:28
Thank you,
I didn't realize the calls would not return me to the loop. I've replaced the GoTo statement with the actual code to be executed and it is working correctly now. Rookie mistake on my part

thank you both for your help.

Happy Holidays
Go to Top of Page
   

- Advertisement -