If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > WAITFOR inside loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-28-03, 11:17
RReynolds RReynolds is offline
Registered User
 
Join Date: Feb 2003
Posts: 6
Question WAITFOR inside loop

Alright, this is dumb but it's driving me crazy!

I am trying to use the WAITFOR DELAY statement in a while loop. I want the loop to run indefinitely and perform some SQL statements periodically. I can get the WAITFOR DELAY to work fine as long as it runs only once but it will not work in the loop. Just for a simple test I wrote the following code and ran it in Query Analyzer:

WHILE 1=1 BEGIN
PRINT 'OK'
WAITFOR DELAY '000:00:05'
END

Nothing is printed and if I cancel the query it simply reports that user canceled the query, go figure. But never any output. However, if I change the delay time to '000:00:00' the loop runs and returns multiple printed rows when canceled. Is there something I'm missing here as far as resetting the delay time or something?
Reply With Quote
  #2 (permalink)  
Old 02-28-03, 12:23
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
try this...

Code:
WHILE 1=1 BEGIN
  raiserror('OK',0,1) with nowait
  WAITFOR DELAY '000:00:05'
END
which works diffrently than...

Code:
WHILE 1=1 BEGIN
  raiserror('OK',0,1)
  WAITFOR DELAY '000:00:05'
END
As I recall the diffrence is that all output is held untill the end of the batch, adding the "with nowait" to the raiserror forced the output to the user.
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #3 (permalink)  
Old 02-28-03, 14:32
RReynolds RReynolds is offline
Registered User
 
Join Date: Feb 2003
Posts: 6
Thanks Paul.

That solved my problem. I placed the "raiserror" code in the loop, along with my code, and now when I cancel the query I get the output.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On