今天实际项目中用到了spool,发现网上好多内容不是很全,自己摸索了好半天,现在总结一下。
一、通过spool 命令,可以将select 数据库的内容写到文件中,通过在sqlplus设置一些参数,使得按指定方式写到文件中
(1)常规使用spool方法,将set的一些命令和spool,select等放入.sql脚本中,然后再sqlplus中运行该脚本。以下为logmnr.sql脚本,
在sqlplus中执行@logmnr.sql就可以写入文件record3.txt中。不会再终端显示任何信息。但是,如果是在sqlplus中输入:
set termout off;
......
spool record3.txt
select ....... from .....;
spool off;
前面的设置是没有用的,还是会在终端中显示大量信息。
set echo off;
set heading off;
set line 100;
set long 2000000000;
set longchunksize 255;
set wra on;
set newpage none;
set pagesize 0;
set numwidth 12;
set termout off;
set trimout on;
set trimspool on;
set feedback off;
set timing on;
execute dbms_logmnr.add_logfile(LogFileName=>'/oracle/app/oracle/logs/hrbfct_1_4156_748575599.arc',Options=>dbms_logmnr.new);
execute dbms_logmnr.add_logfile(LogFileName=>'/oracle/app/oracle/logs/hrbfct_2_6645_748575599.arc',Options=>dbms_logmnr.addfile);
execute dbms_logmnr.start_logmnr(DictFileName=>'/oracle/app/oracle/logs/dict.ora');
spool /oracle/app/oracle/logs/record3.txt;
select to_clob(sql_redo)||'|'||to_char(scn)||'|'||to_char(timestamp)||'|'||to_char(session_info)||'|'||to_char(table_name)||'|'||to_char(seg_owner)||'?'
from v$logmnr_contents;
spool off;
exit;
(2)那到底能否在shell脚本中运行还不显示这些信息呢,答案是有的。
例如
#!/bin/ksh
echo "set echo off;
set heading off;
set line 100;
set long 2000000000;
set longchunksize 255;
set wra on;
set newpage none;
set pagesize 0;
set numwidth 12;
set termout off;
set trimout on;
set trimspool on;
set feedback off;
set timing on;
execute dbms_logmnr.add_logfile(LogFileName=>'/oracle/app/oracle/logs/hrbfct_1_4156_748575599.arc',Options=>dbms_logmnr.new);
execute dbms_logmnr.start_logmnr(DictFileName=>'/oracle/app/oracle/logs/dict.ora');
spool /oracle/app/oracle/logs/record3.txt;
select to_clob(sql_redo)||'|'||to_char(scn)||'|'||to_char(timestamp)||'|'||to_char(session_info)||'|'||to_char(table_name)||'|'||to_char(seg_owner)||'?'
from v\$logmnr_contents;
spool off;
" | sqlplus '/as sysdba'>/dev/null
这样就能利用shell脚本中执行spool方法,同时不会再终端中显示。注意,只有这种方法可以。
试过这种方法,结果证明是不行的。。。。 看着和上面echo进去很像,但事实就是不行,还是会显示大量的信息,两个!就是将中间内容发送到sqlplus中作为输入
#!/bin/bash
......
.....
sqlplus oracleuser/user@SERVICE_NAME << !
set ECHO off
set heading off
set pagesize 0
set linesize 1000
set term off
set trims on
set feedback off
spool $tmpfile
select owner||'.'||table_name||',' from all_tables where owner=upper('$owner_user') and table_name like 'DR%$exp_month%';
spool off
quit
!
.......
(2)spool通常会用到连接||,这里讲一下连接是怎么回事
SQL> SELECT LPAD('x',4000,'x') || LPAD('x',4000,'x') || LPAD('x',4000,'x') FROM DUAL;
SELECT LPAD('x',4000,'x') || LPAD('x',4000,'x') || LPAD('x',4000,'x') FROM DUAL
*
ERROR at line 1:
ORA-01489: result of string concatenation is too long 这里简单先介绍下lpad和rpad是怎么回事:(l,r只是方向不同)
rpad函数从右边对字符串使用指定的字符进行填充
rpad(string,padded_length,[pad_string])
string 表示:被填充的字符串
padded_length 表示:字符的长度,是返回的字符串的数量,如果这个数量比原字符串的长度要短,rpad函数将会把字符串截取成从左到右的n个字符;
pad_string 是个可选参数,这个字符串是要粘贴到string的右边,如果这个参数未写,lpad函数将会在string的右边粘贴空格。
例如:
rpad('tech', 7); 将返回'tech '
rpad('tech', 2); 将返回'te'
rpad('tech', 8, '0'); 将返回'tech0000'
rpad('tech on the net', 15, 'z'); 将返回 'tech on the net'
rpad('tech on the net', 16, 'z'); 将返回 'tech on the netz' 好了,现在回到上面的问题,为什么会出错呢,因为varchar2在oracle中,最多只支持到4000个字符,也就是32K,||的操作会把后面的放入到前面里,就是把后面4000个x放入到
前一个4000个x里,作为varchar2,当然就超过了4000的界限。
Problem Description:
The problem with this query is with the use of CONCAT operator (||). e.g.: select char1 || char2 from dual
Concat operator returns char1 concatenated with char2. The string returned is in the
same character set as char1. So here concat operator is trying to return varchar2,
which has limit of 4000 characters and getting exceeded. This problem may also come when we try to CONCAT a VARCHAR2 with CLOB.
e.g.: select char1 || clob from dual So here we can simply convert its first string to CLOB and avoid this error.
After converting first string to CLOB, CONCAT operator will return string of CLOB type Solution:
SELECT TO_CLOB(LPAD('x',4000,'x')) || LPAD('x',4000,'x') || LPAD('x',4000,'x')
FROM DUAL
所以问题解决了,只需要将连接的第一个转换成clob就可以。 看我上面的logminer.sql,我将sql_redo用to_clob函数转换成了clob类型,如果不设置set long 20000000和set longchunksize 255;就会发现,在record3文档中每行只有
前80个字符,剩下的都被截断了,这就是我上篇博客中的clob截断问题,所以用上篇博客的方法可以完美解决问题。欧耶! (3)shell调用spool 的另一种方法,
那就是在shell中调用
selecttpmof03.txt
set heading off
set feedback off
set echo off
set newp none
set termout off
spool /home/orarun/scripts/date.txt
select a.REC_CREATOR||'|'||a.REC_CREATE_TIME||'|'||b.event_name||'|'||a.ORDER_NO||'|'||a.MAT_NO||'|'||a.MAT_STATUS||'|'||a.WT from tpmof03 a,tpmof21 b where (a.event_id=b.event_id and a.event_id in('','6A','6B','6C','6D','5B'))and (a.rec_create_time>='');
spool off;
1 #!/bin/sh rm /home/orarun/scripts/date.txt sqlplus tjc1/tjc10804@tjc1 << EOF @selecttpmof03.txt #或者sqlplus 。。。。。。。。@logminer.sql EOF
这种方法理论上也不会在终端上显示信息,不知道为啥上面那个用!的就不行,感觉差不多的样子
(4)还有一种想法可以在shell脚本中编写出一个.sql脚本,然后去执行它。
#!/bin/ksh
record=/oracle/app/oracle/logs/dirct
flag=
count=
echo "set echo off;
set heading off;
set line ;
set long ;
set longchunksize ;
set wra on;
set newpage none;
set pagesize ;
set numwidth ;
set termout off;
set trimout on;
set trimspool on;
set feedback off;
set timing on;" > logmnr.sql
echo "write config"
for file_i in `cat $record`;
do
flag=
if [ $count -eq ];then
sed -i '/'''$file_i'''/d' $record
echo "execute dbms_logmnr.add_logfile(LogFileName=>'/oracle/app/oracle/logs/$file_i',Options=>dbms_logmnr.new);">>logmnr.sql
count=
else
sed -i '/'''$file_i'''/d' $record
echo "execute dbms_logmnr.add_logfile(LogFileName=>'/oracle/app/oracle/logs/$file_i',Options=>dbms_logmnr.addfile);">>logmnr.sql
fi
done
echo "execute dbms_logmnr.start_logmnr(DictFileName=>'/oracle/app/oracle/logs/dict.ora');">>logmnr.sql
if [ $flag -eq ];then
echo "spool /oracle/app/oracle/logs/record3.txt;
select to_clob(sql_redo)||'|'||to_char(scn)||'|'||to_char(timestamp)||'|'||to_char(session_info)||'|'||to_char(table_name)||'|'||to_char(seg_owner)||'?'
from v\$logmnr_contents;
spool off;
exit;">>logmnr.sql
#sqlplus '/as sysdba' @logmnr.sql