Creating messages for print and raiserror  Comments

Chapter 14: Using Batches and Control-of-Flow Language

waitfor

The waitfor keyword specifies a specific time of day, a time interval, or an event at which the execution of a statement block, stored procedure, or transaction is to occur.

The syntax is:

waitfor {delay "time" | time "time" | errorexit |  processexit | mirrorexit}

where delay time instructs Adaptive Server to wait until the specified period of time has passed. time time instructs Adaptive Server to wait until the specified time, given in one of the valid formats for datetime data.

However, you cannot specify dates—the date portion of the datetime value is not allowed. The time you specify with waitfor time or waitfor delay can include hours, minutes, and seconds—up to a maximum of 24 hours. Use the format “hh:mm:ss”.

For example, the following command instructs Adaptive Server to wait until 4:23 p.m.:

waitfor time "16:23"

This command instructs Adaptive Server to wait 1 hour and 30 minutes:

waitfor delay "01:30" 

For a review of the formats for time values, see “Entering times”.

errorexit instructs Adaptive Server to wait until a process terminates abnormally. processexit waits until a process terminates for any reason. mirrorexit waits until a read or write to a mirrored device fails.

You can use waitfor errorexit with a procedure that kills the abnormally terminated process to free system resources that would otherwise be taken up by an infected process. To find out which process is infected, use sp_who to check the sysprocesses table.

The following example instructs Adaptive Server to wait until 2:20 p.m. Then it updates the chess table with the next move and executes a stored procedure called sendmessage, which inserts a message into one of Judy’s tables notifying her that a new move now exists in the chess table.

begin 
waitfor time "14:20" 
insert chess(next_move) 
values("Q-KR5") 
execute sendmessage "Judy"
end

To send the message to Judy after 10 seconds instead of waiting until 2:20, substitute this waitfor statement in the preceding example:

waitfor delay "0:00:10" 

After you give the waitfor command, you cannot use your connection to Adaptive Server until the time or event that you specified occurs.





Copyright © 2005. Sybase Inc. All rights reserved. Comments

View this book as PDF