Solved

Stored Procedure WHILE LOOP syntax

Posted on 2010-09-01
9
570 Views
Last Modified: 2012-05-10
Hello Experts,
Need your assistance in creating a stored procedure where it will be
What i was trying to make is the following (in sequence) on my stored procedure...
1.0 Utilizes a WHILE LOOP using one of my table (DOWNLOAD) which is populated by a web application.
DOWNLOAD
DownloadID     DownloadYear  SessionID    CustomerID   CustLoc Status
90000              2010                   10000          A014023      005         1
90001              2010                   10000          A014033      005         1
90002              2010                   10000          A014043      005         1
90003              2010                   10001          A019023      004         NULL
90004              2010                   10001          A018023      004         NULL
90005              2010                   10001          A028023      004         NULL
90006              2010                   10001          A058023      004         NULL
90007              2010                   10001          A048023      004         NULL
90008              2010                   10001          A018333      004         NULL
It will Loop in each record(row) under the specific  'SessionID'  and value of status (=NULL) or only unprocessed. The values (each row) on this table will then be used (e.g. DownloadYear, CustomerID and CustLoc) in the Select statement.
I've attached what i have started but somehow felt something is dreadfully mising if not worng at all.
I guess where i'm stucked now is how to utlize the correct values (?????) on each loop on my Select statement

2.0 Then when the SELECT was successful (all rows have been processed) then it will update the DOWNLOAD table and set the status column to 1 and the date on downloaddate for the particular SessionID.

CREATE PROCEDURE SMI_P100_Student_Download_TEST

@SessionID INT

AS

DECLARE @tab table (i int identity, DownloadID INT, CustomerID VARCHAR(20))

DECLARE @rows int



INSERT INTO @tab

SELECT DownloadID, CustomerID FROM [dbo].[DOWNLOAD] WHERE [SessionID] = @SessionID AND [Status] IS NULL

SET @rows = @@Rowcount



WHILE @rows > 0

BEGIN

SELECT...

FROM...

WHERE...

AND DownloadYear = ?????

AND CustomerID =?????

AND CustLoc = ?????



SET @counter = @counter + 1

END

Open in new window

0
Comment
Question by:jsuanque
  • 6
  • 2
9 Comments
 

Author Comment

by:jsuanque
ID: 33582397
Ooops i forgot to include the column
DOWNLOAD.DownloadDate
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33582752
First do not loop!  That is simply not a good idea.  Next tell us what version you are running so that we can offer the best solution available to you.
0
 

Author Comment

by:jsuanque
ID: 33582783
Currently it will be deployed to SQL Server 2000 but will be moving it mid-next year to SQL Server 2008.
0
 
LVL 11

Accepted Solution

by:
Larissa T earned 250 total points
ID: 33583108
Here is  while loop. Don't have any problems with while. we use it a lot. We are using SQL 2000.
You can use cursors to get the same result, but we have actually rewritten many procedures replacing cursors with those loops for performance reason

If you have some unique value that will identify row in your original table, you can use it directly, without creating @tab table. replace @i with this value
---------------
select @i = 0, @counter=0
WHILE (@counter<@rows)
-- of just WHILE (1=1)
BEGIN
      SELECT top 1 @DownloadID=DownloadID, @CustomerID=CustomerID , @i=i
      FROM @tab WHERE i .> @i
      order by i
      if @@Rowcount =  0      break

      -- you code here

      SET @counter = @counter + 1
END
----------------
0
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.

 

Author Comment

by:jsuanque
ID: 33583148
Hello Trofimoval,
I'll try your suggetsion later since our server HD just died few minutes ago thus will take some time before SQL Server will be online.
0
 

Author Comment

by:jsuanque
ID: 33608209
Hello Trofimoval,
Thanks it did work and seems to be very quick. Anyway as for the second item...

2.0 Then when the SELECT was successful (all rows have been processed) then it will update the DOWNLOAD table and set the status column to 1 and the date on downloaddate for the particular SessionID.

Is it possible to do this? or shall i open another question just for this.

Cheers,
JSuanque
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33608531
Not sure I understand what you try to update. Can you post code you do inside loop?
here is sample

declare @updateDt datetime, @SessionID  int
select @updateDt  = getdate(), @SessionID=0

while (1=1) begin
   select top 1 @sesionID=sesionID from DOWNLOAD where status = null and SessionID>@SessionID
          order by sesionID
  update  [dbo].[DOWNLOAD] set downloaddate =@updateDt , status=1
   WHERE [SessionID] = @SessionID and status = null

end
0
 

Author Comment

by:jsuanque
ID: 33608715
Hello Trofimoval,
Actually i just added the UPDATE statement at the end of the procedure. Thanks.
0
 

Author Closing Comment

by:jsuanque
ID: 33608716
Thanks heaps.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

920 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

16 Experts available now in Live!

Get 1:1 Help Now