Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LOOP within LOOP in stored procedure

Status
Not open for further replies.

moleboy

IS-IT--Management
Oct 10, 2002
33
GB
My code requires two loops, the first is looping through timebands (which is OK) the second moving the @startdate+1 until @enddate

This is the first loop, I've been trying to add a second (@startdate) loop without success (using WHILE @startdate<= @enddate)

my code....

set @timeband = @starttime

WHILE @timeband <= @endtime

begin
UPDATE tblTempAllowance SET bkdexception = (SELECT SUM(tblTempHol.bkdexception)
FROM tblTempHol WHERE timeband=left(convert(char(25), @timeband, 8),5) and Allowancedate= @startdate)
WHERE timeband=left(convert(char(25), @timeband, 8),5) and Allowancedate=@startdate
set @timeband = dateadd(mi, 30, @timeband)
end


Can anyone help me with the syntax??
 
Something like this?

Code:
WHILE @startdate <= @enddate
BEGIN
  SET @timeband = @starttime
  
  WHILE @timeband <= @endtime
  BEGIN
    --do update

    SET @timeband = DATEADD(mi, 30, @timeband)
  END

  SET @startdate = @startdate + 1
END

--James
 
seems to have done the trick,

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top