DB2 PL/SQL Example: Sleep Procedure

fjzcau發表於2015-03-28
Sometimes it's useful to have the possibility to suspend a session for a specific time.
In other languages like Java, C, Shell, etc. is a sleep command available.
But as default there's no sleep command in DB2.
However it's very simple to create a stored procedure with exacly this behaviour:


db2 -td@ "create procedure s1.sleep (in sleeptime integer)
begin
  declare wait_until timestamp;

  set wait_until = (current timestamp + sleeptime seconds);
  while (wait_until > current timestamp)
    do
    end while;
end
@
"

Another option is to wait to a specific timestamp:

create procedure sleep_until (in sleepuntil_timestamp timestamp)
begin
  while (sleepuntil_timestamp > current timestamp)
   do
   end while;
end!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22661144/viewspace-1477097/,如需轉載,請註明出處,否則將追究法律責任。

相關文章