温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PL/SQL中如何让程序每隔几秒插入一条数据

发布时间:2020-03-30 19:50:20 来源:网络 阅读:5479 作者:xjsunjie 栏目:关系型数据库

在编写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 <<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;


SQL> select id,hostname,to_char(datetime,'YYYY-MM-DD HH24:MI:SS') from test;

         1 oraclevm 2014-12-27 10:19:09

         2 oraclevm 2014-12-27 10:19:10

         3 oraclevm 2014-12-27 10:19:11

         4 oraclevm 2014-12-27 10:19:12

         5 oraclevm 2014-12-27 10:19:13

         6 oraclevm 2014-12-27 10:19:14

         7 oraclevm 2014-12-27 10:19:15

         8 oraclevm 2014-12-27 10:19:16

         9 oraclevm 2014-12-27 10:19:17

        10 oraclevm 2014-12-27 10:19:18

        


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 off


2. 在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语句。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI