Kingbase V8R6存储过程变量数据导出到操作系统文件
说明:
KingbaseES V8R6如何将自定义过程中的变量数据导出到操作系统文件中。
本次案例数据库版本:
test=# select version();
version
----------------------------------------------------------------------------------------------------------------------
KingbaseES V008R006C005B0023 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
(1 行记录)
1.通过copy方式导出:
COPY命令格式:
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
[ WHERE condition ]
注意:copy xxx命令格式,xxx参数不支持直接传入变量数据,只能从表中复制数据
\set SQLTERM /
declare
out_data varchar2(30000);
copysql varchar;
fild_name varchar2(1000) default 'copy_file';
begin
out_data := '<?xml version="1.0"?><PACKAGEHEAD></PACKAGEHEAD><PACKAGEBODY></PACKAGEBODY>' ;
-- 创建临时表,将变量数据写入临时表,作为copy操作过渡
create temporary table if not exists xmltemp(xmltext text );
insert into xmltemp select out_data || '<PACKAGEBODY></PACKAGEBODY>' from dual;
copysql := ' copy ( select xmltext from xmltemp ) to '''|| '/home/kingbase/exptable/' || fild_name ||''' ' ;
execute immediate copysql;
delete from xmltemp;
end;
/
[kingbase@node128 exptable]$ ll
总用量 4
-rw-r--r-- 1 kingbase kingbase 103 8月 19 13:47 copy_file
[kingbase@node128 exptable]$ more copy_file
<?xml version="1.0"?>
<PACKAGEHEAD></PACKAGEHEAD>
<PACKAGEBODY></PACKAGEBODY>
2.通过utl_file扩展导出:
1.使用前需要在shared-library中添加utl_file一项。使用时需要create extension utl_file。
不需要使用时drop extension utl_file即可。
2.调用文件写入扩展
\set SQLTERM /
declare
out_data varchar2(30000);
file_handle utl_file.file_type;
fild_name varchar2(1000) default 'utl_file';
begin
out_data := '<?xml version="1.0"?><PACKAGEHEAD></PACKAGEHEAD><PACKAGEBODY></PACKAGEBODY>' ;
file_handle := utl_file.fopen('/home/kingbase/exptable', fild_name, 'a', 30000);
utl_file.put_line(file_handle, out_data);
utl_file.fclose(file_handle);
end;
/
[kingbase@node128 exptable]$ ll
总用量 8
-rw-r--r-- 1 kingbase kingbase 103 8月 19 13:47 copy_file
-rw------- 1 kingbase kingbase 76 8月 19 13:48 utl_file
[kingbase@node128 exptable]$ more utl_file
<?xml version="1.0"?>
<PACKAGEHEAD></PACKAGEHEAD>
<PACKAGEBODY></PACKAGEBODY>
[kingbase@node128 exptable]$
标签:操作系统,V8R6,utl,exptable,kingbase,file,Kingbase,copy,name
From: https://www.cnblogs.com/kingbase/p/16602968.html