Solved

Is there a "Wait" command in Oracle?

Posted on 1999-01-29
5
4,224 Views
Last Modified: 2008-03-10
I want to submit a SQL query to an Oracle database which I know will not come back for a while (preferably without bad style like running a really inefficient join). I know how to do this in Sybase, but having searched the Oracle website, I can not find such a command. In Sybase, the command is:
   waitfor delay "0:01:00"
to cause a Sybase TSQL call to pause for 1 minute, then continue execution. Obviously, this is a Sybase enhancement to ANSI SQL. Is there any similar command in Oracle SQL? In a worst case, does Oracle SQL have control-of-flow logic which would allow it to sit in a loop until a particular time?

Thanks in advance.
0
Comment
Question by:parkerea
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
dslavin earned 70 total points
Comment Utility
Here is a stored procedure that will provide a wait of a specified number of seconds:

-- Package to hold 'sleep' procedure and other utilities
create or replace package user_util
IS
   PROCEDURE sleep (num_of_sec IN NUMBER);
END user_util;
/

create or replace package body user_util
IS
  -- Procedure to cause Oracle to sleep NUM_OF_SEC seconds.  Can be invoked before
  -- any other call to provide a wait.
  PROCEDURE sleep (num_of_sec IN NUMBER)
  IS
     start_time NUMBER;
     curr_time NUMBER;
     end_time NUMBER;
  BEGIN
     -- Get current time (SSSSS format returns seconds since midnight)
     -- Thus this routine will not work if started just before midnight
     -- but it could be enhanced to support that as well.
     select to_number(to_char(sysdate,'SSSSS')) into start_time from dual;

     -- Calculate end of sleep time
     end_time := start_time + num_of_sec;

     -- Loop until End Time is reached
     WHILE (1=1) LOOP
       -- Get current time
       select to_number(to_char(sysdate,'SSSSS')) into curr_time from dual;

       -- Compare to end time and exit if time is past
       IF (curr_time >= end_time) THEN
          EXIT;
       END IF;
     END LOOP;
  END sleep;
END user_util;
/

Hope this helps.

0
 
LVL 2

Expert Comment

by:dslavin
Comment Utility
The above procedure can be invoked like this:

   user_util.sleep (10);

   Sleeps for 10 seconds.

P.S.:  You cannot easily specify a wait of less than a second.  It can be done but requires a lot more work.  There was a PAQ regarding this which you can look into for more details.
0
 
LVL 2

Expert Comment

by:dslavin
Comment Utility
Also, If you don't like polling in a loop, then you can:

1) Open an Oracle Pipe
2) Try to receive a message (that noone will send).  One of the parameters to specify on the pipe message receive command is the number of seconds to wait for a message.  So you can set this to how many seconds you want to wait.  Then when the command finishes, your wait is up.  It would be something like this:

PROCEDURE sleep (SLEEP_TIME IN NUMBER)
IS
   status INTEGER;

BEGIN
   -- Create pipe and purge any existing messages
   status := DBMS_PIPE.CREATE_PIPE ('sleep_pipe', 8192, TRUE);
   DBMS_PIPE.RESET_BUFFER;
   DBMS_PIPE.PURGE ('sleep_pipe');

   -- Wait to receive message that will never come
   status := DBMS_PIPE.RECEIVE_MESSAGE ('sleep_pipe', SLEEP_TIME);

   -- Delete pipe
   status := DBMS_PIPE.REMOVE_PIPE ('sleep_pipe');

END;

You can add error checking using the 'status' returned values to make this more solid.
   
0
 

Author Comment

by:parkerea
Comment Utility
Thanks -- just the sort of thing I was looking for.
0
 

Author Comment

by:parkerea
Comment Utility
Oops... I intended to accept your answer, but accidentally added a comment.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now