create or replace package unloader
AUTHID CURRENT_USER
as
/* Function run -- unloads data from any query into a file
and creates a control file to reload that
data into another table
p_query = SQL query to "unload". May be virtually any query.
p_tname = Table to load into. Will be put into control file.
p_mode = REPLACE|APPEND|TRUNCATE -- how to reload the data
p_dir = directory we will write the ctl and dat file to.
p_filename = name of file to write to. I will add .ctl and .dat
to this name
p_separator = field delimiter. I default this to a comma.
p_enclosure = what each field will be wrapped in
p_terminator = end of line character. We use this so we can unload
and reload data with newlines in it. I default to
"|\n" (a pipe and a newline together) and "|\r\n" on NT.
You need only to override this if you believe your
data will have that sequence in it. I ALWAYS add the
OS "end of line" marker to this sequence, you should not
*/
function run( p_query in varchar2,
p_tname in varchar2,
p_mode in varchar2 default 'REPLACE',
p_dir in varchar2,
p_filename in varchar2,
p_separator in varchar2 default ',',
p_enclosure in varchar2 default '"',
p_terminator in varchar2 default '|' )
return number;
end;
/
create or replace package body unloader
as
g_theCursor integer default dbms_sql.open_cursor;
g_descTbl dbms_sql.desc_tab;
g_nl varchar2(2) default chr(10);
function to_hex( p_str in varchar2 ) return varchar2
is
begin
return to_char( ascii(p_str), 'fm0x' );
end;
function is_windows return boolean
is
l_cfiles varchar2(4000);
l_dummy number;
begin
if (dbms_utility.get_parameter_value( 'control_files', l_dummy, l_cfiles )>0)
then
return instr( l_cfiles, '\' ) > 0;
else
return FALSE;
end if;
end;
procedure dump_ctl( p_dir in varchar2,
p_filename in varchar2,
p_tname in varchar2,
p_mode in varchar2,
p_separator in varchar2,
p_enclosure in varchar2,
p_terminator in varchar2 )
is
l_output utl_file.file_type;
l_sep varchar2(5);
l_str varchar2(5) := chr(10);
begin
if ( is_windows )
then
l_str := chr(13) || chr(10);
end if;
l_output := utl_file.fopen( p_dir, p_filename || '.ctl', 'w' );
utl_file.put_line( l_output, 'load data' );
utl_file.put_line( l_output, 'infile ''' ||
p_filename || '.dat'' "str x''' ||
utl_raw.cast_to_raw( p_terminator ||
l_str ) || '''"' );
utl_file.put_line( l_output, 'into table ' || p_tname );
utl_file.put_line( l_output, p_mode );
utl_file.put_line( l_output, 'fields terminated by X''' ||
to_hex(p_separator) ||
''' enclosed by X''' ||
to_hex(p_enclosure) || ''' ' );
utl_file.put_line( l_output, '(' );
for i in 1 .. g_descTbl.count
loop
if ( g_descTbl(i).col_type = 12 )
then
utl_file.put( l_output, l_sep || g_descTbl(i).col_name ||
' date ''ddmmyyyyhh24miss'' ');
else
utl_file.put( l_output, l_sep || g_descTbl(i).col_name ||
' char(' ||
to_char(g_descTbl(i).col_max_len*2) ||' )' );
end if;
l_sep := ','||g_nl ;
end loop;
utl_file.put_line( l_output, g_nl || ')' );
utl_file.fclose( l_output );
end;
function quote(p_str in varchar2, p_enclosure in varchar2)
return varchar2
is
begin
return p_enclosure ||
replace( p_str, p_enclosure, p_enclosure||p_enclosure ) ||
p_enclosure;
end;
function run( p_query in varchar2,
p_tname in varchar2,
p_mode in varchar2 default 'REPLACE',
p_dir in varchar2,
p_filename in varchar2,
p_separator in varchar2 default ',',
p_enclosure in varchar2 default '"',
p_terminator in varchar2 default '|' ) return number
is
l_output utl_file.file_type;
l_columnValue varchar2(4000);
l_colCnt number default 0;
l_separator varchar2(10) default '';
l_cnt number default 0;
l_line long;
l_datefmt varchar2(255);
l_descTbl dbms_sql.desc_tab;
begin
select value
into l_datefmt
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
/*
Set the date format to a big numeric string. Avoids
all NLS issues and saves both the time and date.
*/
execute immediate
'alter session set nls_date_format=''ddmmyyyyhh24miss'' ';
/*
Set up an exception block so that in the event of any
error, we can at least reset the date format.
*/
begin
/*
Parse and describe the query. We reset the
descTbl to an empty table so .count on it
will be reliable.
*/
dbms_sql.parse( g_theCursor, p_query, dbms_sql.native );
g_descTbl := l_descTbl;
dbms_sql.describe_columns( g_theCursor, l_colCnt, g_descTbl );
/*
Create a control file to reload this data
into the desired table.
*/
dump_ctl( p_dir, p_filename, p_tname, p_mode, p_separator,
p_enclosure, p_terminator );
/*
Bind every single column to a varchar2(4000). We don't care
if we are fetching a number or a date or whatever.
Everything can be a string.
*/
for i in 1 .. l_colCnt loop
dbms_sql.define_column( g_theCursor, i, l_columnValue, 4000);
end loop;
/*
Run the query - ignore the output of execute. It is only
valid when the DML is an insert/update or delete.
*/
l_cnt := dbms_sql.execute(g_theCursor);
/*
Open the file to write output to and then write the
delimited data to it.
*/
l_output := utl_file.fopen( p_dir, p_filename || '.dat', 'w',
32760 );
loop
exit when ( dbms_sql.fetch_rows(g_theCursor) <= 0 );
l_separator := '';
l_line := null;
for i in 1 .. l_colCnt loop
dbms_sql.column_value( g_theCursor, i,
l_columnValue );
l_line := l_line || l_separator ||
quote( l_columnValue, p_enclosure );
l_separator := p_separator;
end loop;
l_line := l_line || p_terminator;
utl_file.put_line( l_output, l_line );
l_cnt := l_cnt+1;
end loop;
utl_file.fclose( l_output );
/*
Now reset the date format and return the number of rows
written to the output file.
*/
execute immediate
'alter session set nls_date_format=''' || l_datefmt || '''';
return l_cnt;
exception
/*
In the event of ANY error, reset the data format and
re-raise the error.
*/
when others then
execute immediate
没有合适的资源?快使用搜索试试~ 我知道了~
Expert Oracle Database Architecture 9i and 10g Programming Techn...
共152个文件
sql:139个
log:3个
ctl:2个
5星 · 超过95%的资源 需积分: 10 115 下载量 130 浏览量
2007-10-06
19:39:45
上传
评论
收藏 82KB RAR 举报
温馨提示
Expert Oracle Database Architecture 9i and 10g Programming Techniques and Solutions - Source Code
资源推荐
资源详情
资源评论
收起资源包目录
Expert Oracle Database Architecture 9i and 10g Programming Techniques and Solutions - Source Code (152个子文件)
instest.java.BINDS 681B
demo02.control_files 4KB
demo04.control_files 1KB
big_table.ctl 263B
demo1.ctl 186B
big_table.log 5KB
demo01.log 2KB
demo1.log 274B
instest.java.NOBIND 639B
tk.prf 7KB
README 1KB
demo07.sql 9KB
show_space.sql 4KB
runstats.sql 4KB
demo08.sql 4KB
demo10.sql 4KB
demo04.sql 3KB
demo11.sql 3KB
demo03.sql 3KB
demo12.sql 3KB
demo09.sql 3KB
demo06.sql 2KB
demo04.sql 2KB
emp_dept_build.sql 2KB
demo02.sql 2KB
demo11.sql 2KB
demo07.sql 2KB
demo13.sql 2KB
demo07.sql 2KB
demo08.sql 2KB
demo05.sql 2KB
demo13.sql 2KB
demo11.sql 2KB
demo10.sql 2KB
demo04.sql 2KB
demo12.sql 2KB
demo03.sql 2KB
demo07.sql 1KB
demo06.sql 1KB
demo06.sql 1KB
demo03.sql 1KB
demo03.sql 1KB
demo06.sql 1KB
demo04.sql 1KB
demo12.sql 1KB
demo07.sql 1KB
demo09.sql 1KB
demo05.sql 1KB
demo14.sql 1KB
demo06.sql 1KB
demo08.sql 1KB
demo07.sql 1KB
big_table.sql 1KB
demo05.sql 1KB
demo17.sql 1KB
demo04.sql 1KB
demo06.sql 1KB
demo08.sql 1KB
demo01.sql 1016B
demo07.sql 1007B
demo02.sql 984B
demo04.sql 979B
demo03.sql 899B
demo09.sql 885B
demo03.sql 857B
demo03.sql 800B
watch_stat_2.sql 794B
demo02.sql 785B
demo12.sql 764B
demo10.sql 746B
demo05.sql 723B
demo01.sql 719B
demo05.sql 712B
demo05.sql 693B
demo09.sql 670B
demo03.sql 666B
demo10.sql 663B
demo01.sql 661B
demo06.sql 660B
demo09.sql 656B
watch_stat.sql 633B
demo08.sql 628B
demo05.sql 586B
demo16.sql 567B
demo04.sql 563B
demo02.sql 562B
demo13.sql 544B
demo11.sql 528B
demo01.sql 527B
demo07.sql 525B
demo01.sql 514B
login.sql 497B
demo01.sql 493B
demo02.sql 475B
demo05.sql 466B
demo01.sql 457B
demo14.sql 453B
demo11.sql 447B
demo12.sql 434B
demo05.sql 432B
共 152 条
- 1
- 2
资源评论
- lhf42015-06-01很好的教程
ZhouZhiqiangFreesky
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- yolov5,SSD 可能使用到的一些代码
- 基于c51单片机+DS1302+DHT11温湿度模块+LCD1602显示的万年历硬件原理图+BOM+软件程源码序+仿真图.zip
- NSGA2的MATLAB代码
- Messagepassingtest_GCN_DGL.py
- Sh,Docker 运维好帮手,一招通过 sh 脚本批量快速启动和重启多个Docker 容器
- PCF2123.pdf
- 打开注册表操作.doc
- Windows 常见运行运行库32+64
- WMJUL8iC.html
- 基于3KW光伏并网单相逆变器设计(TMS320F28035控制板+显示板+STM32F103功率板)硬件(原理图+PCB)工程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功