ORACLE 中的睡眠功能

2021-12-06 00:00:00 sql oracle11g oracle plsql

我需要在 ORACLE 中执行 SQL 查询需要一定的时间.所以我写了这个函数:

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_  IN  NUMBER
)
RETURN INTEGER IS
 BEGIN
   DBMS_LOCK.sleep(seconds => TIME_);
RETURN 1;
 EXCEPTION
   WHEN OTHERS THEN
   RAISE;
   RETURN 1;
END TEST_SLEEP;

我就是这样调用的

SELECT TEST_SLEEP(10.5) FROM DUAL

但为了工作,我需要将 DBMS_LOCK 设置授予过程的所有者.

but to work I need set grant of DBMS_LOCK to the owner of the procedure.

如何在不使用 DBMS_LOCK.sleep 函数的情况下重写此函数?

How I can rewrite this function without using the DBMS_LOCK.sleep function?

推荐答案

如果没有授予对 DBMS_LOCK.sleep 的访问权限,这会起作用,但这是一个可怕的黑客:

Short of granting access to DBMS_LOCK.sleep, this will work but it's a horrible hack:

IN_TIME INT; --num seconds
v_now DATE;

-- 1) Get the date & time 
SELECT SYSDATE 
  INTO v_now
  FROM DUAL;

-- 2) Loop until the original timestamp plus the amount of seconds <= current date
LOOP
  EXIT WHEN v_now + (IN_TIME * (1/86400)) <= SYSDATE;
END LOOP;

相关文章