在编写Oracle PL/SQL中,如果需要程序执行中暂停几秒钟再继续执行,可以通过Oracle内置的dbms_lock.sleep来实现,不过dbms_lock包需要用户自己安装。 [root@oraclevm ~]# su - oracle [oracle@oraclevm ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Mon May 25 16:36:12 2015Copyright (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.sqlPackage created.Synonym created.Grant succeeded.SQL> grant execute on dbms_lock to public; --授权PUBLIC执行权限
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 <<EOF sqlplus / as sysdba <<EOF drop table test; drop sequence test_seq; create table test (id int,hostname varchar2(50),datetime date); create sequence test_seq minvalue 1 maxvalue 100000 start with 1 increment by 1 cache 20; declare maxrecords constant int:=100000; i int :=1; begin for i in 1..maxrecords loop insert into test (id,hostname,datetime) values (test_seq.nextval,"oraclevm",sysdate); commit; dbms_lock.sleep(1); end loop; end; / exit; EOF[root@oraclevm ~]#chmod 755 /tmp/11.sh [root@oraclevm ~]#cd /tmp [root@oraclevm ~]#./11.sh 即可执行。先设置一下时间格式 export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"最后进行数据查询 spool /tmp/test_oracle set linesize 80 col hostname format a8; set colsep" "; set pagesize 0;select to_char(datetime,"YYYY-MM-DD HH24:MI:SS") from test where hostname="hostname" order by id;spool用法: spool是Oracle SQL*PLUS下的命令,可以用它来导出表中的大量数据,生成格式可以由自己手动控制。 1. 编写spool.sql脚本,存放在/tmp目录下 set trimspool on set linesize 120 set pagesize 2000 set newpage 1 set heading off set term off set echo off set feedback off spool /tmp/data.txt select deptno || "," || dname || "," || loc from dept; spool off2. 在sql*plus上用scott用户登录,执行上面的sql脚本 !/tmp/spool.sql(linux下) @d:spool.sql(windows下)3.观察相应目录,生成了data.txt文件,txt中的内容如下: 10,ACCOUNTING,NEW YORK 20,RESEARCH,DALLAS 30,SALES,CHICAGO 40,OPERATIONS,BOSTON总结: 利用spool可以方便地导出所需要的表,进而可以利用导出的数据进行不同数据库间的数据迁移。 下面介绍使用spool的一些常用设置: set colsep " "; //域输出分隔符 set echo off; //显示start启动的脚本中的每个sql命令,缺省为on set feedback off; //回显本次sql命令处理的记录条数,缺省为on set heading off; //输出域标题,缺省为on set pagesize 0; //输出每页行数,缺省为24,为了避免分页,可设定为0。 set termout off; //显示脚本中的命令的执行结果,缺省为on(可以缩写为term) set trimout on; //去除标准输出每行的拖尾空格,缺省为off set trimspool on; //去除重定向(spool)输出每行的拖尾空格,缺省为off注意: 如果直接在sql*plus中执行上面的spool.sql命令,而不通过执行脚本来进行,那么在生成的txt文件中会存在执行的sql语句。PL/SQL Developer实用技巧分享 http://www.linuxidc.com/Linux/2014-09/107391.htmOracle PL/SQL复合数据类型 http://www.linuxidc.com/Linux/2014-11/109272.htmOracle | PL/SQL Check约束用法详解 http://www.linuxidc.com/Linux/2014-11/109303.htmPL/SQL 存储函数和存储过程 http://www.linuxidc.com/Linux/2014-11/109750.htm更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址