在编写Oracle PL/SQL中,如果需要程序执行中暂停几秒钟再继续执行,可以通过Oracle内置的dbms_lock.sleep来实现,不过dbms_lock包需要用户自己安装。
[root@oraclevm ~]# su - oracle
[oracle@oraclevm ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Mon May 25 16:36:12 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> startup
SQL> @?/rdbms/admin/dbmslock.sql
Package created.
Synonym created.
Grant succeeded.
SQL> grant execute on dbms_lock to public; --授权PUBLIC执行权限
Grant succeeded.
SQL> create table test1(id number,name varchar2(40),time date); --创建test1临时表
Table created.
SQL> select * from test1; --无数据
no rows selected
SQL> SET TIMING ON --打开时间显示
SQL> begin --开始执行测试脚本
2 insert into test1(id,name,time) values(1,'Andy',sysdate);
3 DBMS_LOCK.SLEEP(10); --让程序暂时10秒钟
4 insert into test1(id,name,time) values(2,'Shirley',sysdate);
5 commit;
6 end;
7 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:10.04 --程序执行时间为10.04秒
SQL> SELECT ID,NAME,TO_CHAR(TIME,'YYYY/MM/DD HH24:MI:SS') AS TIME FROM TEST1;
--查询执行结果
ID NAME TIME
---------- ------------------------- -----------------
1 Andy 2014/12/10 10:09:03 --第一条的插入时间是09:03
2 Shirley 2014/12/10 10:09:13 --第二条的插入时间是09:13
刚好比第一条晚了10秒钟
SQL> drop table test1;
Table dropped.
下面给个例子:
每隔一秒插入一条数据
vi /tmp/11.sh
#/bin/sh
su - oracle