Sleep function in ORACLE

后端 未结 11 1875
一整个雨季
一整个雨季 2020-11-30 00:52

I need execute an SQL query in ORACLE it takes a certain amount of time. So I wrote this function:

CREATE OR REPLACE FUNCTION MYSCHEMA.TEST_SLEEP
(
TIME_  I         


        
相关标签:
11条回答
  • 2020-11-30 01:21

    Seems the java procedure/function could work. But why don't you compile your function under a user like the application schema or a admin account that has this grant and just grant your developer account execute on it. That way the definer rights are used.

    0 讨论(0)
  • 2020-11-30 01:22

    If executed within "sqlplus", you can execute a host operating system command "sleep" :

    !sleep 1
    

    or

    host sleep 1
    
    0 讨论(0)
  • 2020-11-30 01:25

    If Java is installed on your 11G then you can do it in a java class and call it from your PL/SQL, but I am not sure that it does not require also a specific grant to call java.

    0 讨论(0)
  • 2020-11-30 01:25

    You can use the DBMS_ALERT package as follows:

    CREATE OR REPLACE FUNCTION sleep(seconds IN NUMBER) RETURN NUMBER
    AS
        PRAGMA AUTONOMOUS_TRANSACTION;
        message VARCHAR2(200);
        status  INTEGER;
    BEGIN
        DBMS_ALERT.WAITONE('noname', message, status, seconds);
        ROLLBACK;
        RETURN seconds;
    END;
    
    SELECT sleep(3) FROM dual;
    
    0 讨论(0)
  • 2020-11-30 01:26

    There is a good article on this topic: PL/SQL: Sleep without using DBMS_LOCK that helped me out. I used Option 2 wrapped in a custom package. Proposed solutions are:

    Option 1: APEX_UTIL.sleep

    If APEX is installed you can use the procedure “PAUSE” from the publicly available package APEX_UTIL.

    Example – “Wait 5 seconds”:

    SET SERVEROUTPUT ON ;
    BEGIN
        DBMS_OUTPUT.PUT_LINE('Start ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
        APEX_UTIL.PAUSE(5);
        DBMS_OUTPUT.PUT_LINE('End   ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
    END;
    /
    

    Option 2: java.lang.Thread.sleep

    An other option is the use of the method “sleep” from the Java class “Thread”, which you can easily use through providing a simple PL/SQL wrapper procedure:

    Note: Please remember, that “Thread.sleep” uses milliseconds!

    --- create ---
    CREATE OR REPLACE PROCEDURE SLEEP (P_MILLI_SECONDS IN NUMBER) 
    AS LANGUAGE JAVA NAME 'java.lang.Thread.sleep(long)';
    
    --- use ---
    SET SERVEROUTPUT ON ;
    BEGIN
        DBMS_OUTPUT.PUT_LINE('Start ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
        SLEEP(5 * 1000);
        DBMS_OUTPUT.PUT_LINE('End   ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
    END;
    /
    
    0 讨论(0)
提交回复
热议问题