spool_sqlldr

--0.制表符chr(9) 换行符chr(10) 回车符chr(13) windows下批处理.bat linux下批处理.sh
set echo off
set feedback off
set pagesize 0
set trimspool on
set trimout on
set colsep ,
SET LINESIZE 2500
set heading off
SPOOL g:\data\t_test.ctl
select 'a,'||chr(10)||'b' from t where rownum<=100;
SPOOL OFF;

--案例1,(导出exel时,文件名后缀为.csv(逗号分隔让exel和txt互相转化),varchar类型加''''||前缀,导出的exel设置成文本格式,对每列在文本编辑器中取出'后重新复制粘贴;
--colsep指定分隔符,可以是逗号或<等分隔符)
--以下在winndows cmd中测试,生成的文件中仍然会有脚本开始和结束提示
--1.通过rpad让列对齐
set echo off
set heading off
set termout off
set feedback off
set pagesize 0
set trimspool on
set trimout on
set colsep ,
SET LINESIZE 2500
set heading off
SPOOL e:\scgd\划帐信息.txt
select rpad(a.划帐信息,20),rpad(b.格式描述,50),rpad(a.帐户类型,10),rpad(a.送盘格式,10) from 划帐信息 a, 划帐格式 b where a.返盘格式 = b.划帐格式;
SPOOL OFF;

--2.
--sqluldr2第三方工具导出文本,然后用sqlldr导入
--导出数据,tables选项会默认生成sqlldr所用的ctl文件
sqluldr2.exe USER=test/test@gbk11g QUERY="select /*+ parallel(2) */ * from t" table=t head=yes FILE=g:\data\t.txt

--将数据加载到数据库中 skip=1表示跳过第一行,从第二行开始导入
sqlldr jms/jms@tiod control=ent_person_sqlldr.ctl log=ent_person_sqlldr.log bad=ent_person_sqlldr_bad.log skip=1 errors=5000 rows=5000 bindsize=335542

--案例2,导出表的inser语句,或用pl/sql导出表的查询语句
set echo off
set feedback off
set pagesize 0
set trimspool on
set trimout on
set colsep ,
SET LINESIZE 2500
set heading off
spool f:\t30_report.sql
select 'insert into t30_report values ('''||reportkey||''','''||effdt||''','''||reportname||''','''||reporttype||''','''||platform||''','''||flag||''','''||deployflag||''','''||descr||''','''||modifier||''','''||modifydate||''','''||creator||''','''||createdate||''','''||frequency||''','''||isrowfixup||''','''||filluser||''','''||checkuser||''','''||principaluser||''','||callevel||');
commit;' from t30_report t where t.reportkey like 'Z1%' and rownum<=100;
spool off;

--案例3,SQLLDR,装载文本数据(也可以是excel文件)
--SS0_A3410.dat数据格式为
41758,01,0101,999,34q24,24520000.000,9400000.000
41758,01,0101,999,34q27,7695840.000,2000000.000
41758,01,0101,999,34q29,7300000.000,4000000.000
或(用""包裹字符串)
"41758","01","0101","999","34q24","24520000.000","9400000.000"
"41758","01","0101","999","34q27","7695840.000","2000000.000"
"41758","01","0101","999","34q29","7300000.000","4000000.000"

--关键字 replace/truncate/insert into(应=append) excel应转化为csv格式,否则有乱码等异常
--若最后1个字段识别长度超长,则用terminated by whtitespace
--SS0_A3410.ctl 装载文本文件的格式 装载csv时去掉terminated by whtitespace
load data
infile "F:\sqlldr\SS0_A3410.dat"
badfile 'f:\sqlldr\SS0_A3410.bad'
replace into table SS0_A3410
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(
JGM,
HBH,
KMH,
JYE,
DYE,
NF terminated by whtitespace
)
--然后
sqlldr userid=ods/ods@srp control=f:\sqlldr\SS0_A3410.ctl log=f:\sqlldr\SS0_A3410.log

--参考 *************************************************
trailing nullcols
(
JGM POSITION (*) CHAR BY ",",
HBH POSITION (*) CHAR BY ",",
KMH POSITION (*) CHAR BY ",",
JYE POSITION (*) CHAR BY ",",
DYE POSITION (*) CHAR BY ",",
NF POSITION (*) CHAR BY ","
)

--生成sqlldr的控制文件 echo后或可以跟' >重定向 通过linux环境的echo生成N个ctl控制文件
select t.be_reportkey,t.datadt,t.reportkey,
'host sqlldr userid=srp/srp@srp control=D:\sqlldr\104zh_pdf_zl\'||t.be_reportkey||'_'||t.datadt||'.ctl log=D:\sqlldr\104zh_pdf_zl\log\'||t.be_reportkey||'_'||t.datadt||'.log'||chr(13)||'update T64_1104ZH_ITEM_TRANS_TMP t1 set t1.reportkey='''||t.reportkey||''',t1.be_reportkey='''||t.be_reportkey||''',t1.organkey=''d9993'',t1.datadt='''||t.datadt||''' where t1.reportkey is null;'||chr(13)||'commit;',
'echo "load data infile ''D:\data\pdf_zl\'||t.be_reportkey||'_'||t.datadt||'.DAT'' badfile ''D:\data\pdf_zl\bad\'||t.be_reportkey||'_'||t.datadt||'.bad'' append into table srp.T64_1104ZH_ITEM_TRANS_TMP fields terminated by '','' optionally enclosed by ''\"'' trailing nullcols (ROW_NUM,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH)" > /zyj/ctl/1104zh/pdf_zl/'||t.be_reportkey||'_'||t.datadt||'.ctl'
from reportkey_bereportkey_pdf t where t.flag='2' order by t.datadt,t.be_reportkey;

--案例4,导出所有存储过程
sqlplus ods/ods@srp

set echo off
set pagesize 0
set trimspool on
set colsep ,
SET LINESIZE 250
set heading off
set feedback off;
spool f:\procedure_spool.sql
select DECODE(T.line,1,REPLACE(T.TEXT,'PROCEDURE','CREATE OR REPLACE PROCEDURE'),T.TEXT) AS TEXT from user_source t where t.TYPE='PROCEDURE' and rownum<=1000;
spool off;

posted @ 2019-04-22 09:13  virtual_daemon  阅读(265)  评论(0编辑  收藏  举报