Solved

Stored Procedure WHILE LOOP syntax

Posted on 2010-09-01
9
569 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
Comment Utility
Ooops i forgot to include the column
DOWNLOAD.DownloadDate
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
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
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:jsuanque
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hello Trofimoval,
Actually i just added the UPDATE statement at the end of the procedure. Thanks.
0
 

Author Closing Comment

by:jsuanque
Comment Utility
Thanks heaps.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

743 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