• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

Stored Procedure WHILE LOOP syntax

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
jsuanque
Asked:
jsuanque
  • 6
  • 2
1 Solution
 
jsuanqueAuthor Commented:
Ooops i forgot to include the column
DOWNLOAD.DownloadDate
0
 
Anthony PerkinsCommented:
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
 
jsuanqueAuthor Commented:
Currently it will be deployed to SQL Server 2000 but will be moving it mid-next year to SQL Server 2008.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Lara FEACommented:
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
 
jsuanqueAuthor Commented:
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
 
jsuanqueAuthor Commented:
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
 
Lara FEACommented:
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
 
jsuanqueAuthor Commented:
Hello Trofimoval,
Actually i just added the UPDATE statement at the end of the procedure. Thanks.
0
 
jsuanqueAuthor Commented:
Thanks heaps.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now