求高手指点oracle数据库里多张表导出xml文件

joancq 2015-02-08 10:03:39
怎样可以将oracle数据库里的表导出xml格式文档?由用户指定从哪些表中导出,所以表名为变量。
...全文
684 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
joancq 2015-03-11
  • 打赏
  • 举报
回复
引用 12 楼 wmxcn2000 的回复:
[quote=引用 11 楼 joancq 的回复:] [quote=引用 10 楼 wmxcn2000 的回复:] 1. 你把 m_rowinfo varchar2(2000) 的长度改成 32000 ,应该太短了 2.确认你这张表,没有 lob 类型的字段。
确定表里都没有lob类型,长度改了,但是仍然报错,列数较少的没问题,但是实验了几个列数20多的表都报错。[/quote] 行数多了,也报错吗?[/quote] 行数试了20多行,列数10内的没有问题,但是行数少,列数多的报错。
卖水果的net 2015-03-11
  • 打赏
  • 举报
回复
1. 你把 m_rowinfo varchar2(2000) 的长度改成 32000 ,应该太短了 2.确认你这张表,没有 lob 类型的字段。
卖水果的net 2015-03-11
  • 打赏
  • 举报
回复
引用 11 楼 joancq 的回复:
[quote=引用 10 楼 wmxcn2000 的回复:] 1. 你把 m_rowinfo varchar2(2000) 的长度改成 32000 ,应该太短了 2.确认你这张表,没有 lob 类型的字段。
确定表里都没有lob类型,长度改了,但是仍然报错,列数较少的没问题,但是实验了几个列数20多的表都报错。[/quote] 行数多了,也报错吗?
joancq 2015-03-11
  • 打赏
  • 举报
回复
引用 10 楼 wmxcn2000 的回复:
1. 你把 m_rowinfo varchar2(2000) 的长度改成 32000 ,应该太短了 2.确认你这张表,没有 lob 类型的字段。
确定表里都没有lob类型,长度改了,但是仍然报错,列数较少的没问题,但是实验了几个列数20多的表都报错。
卖水果的net 2015-03-10
  • 打赏
  • 举报
回复
你能把表和你最终的语句,发上来吗 ? 如果不涉密的话。放在下载里,我下载下来看看。
卖水果的net 2015-03-10
  • 打赏
  • 举报
回复
明天去单位的机器看看,家里没有环境。
joancq 2015-03-10
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
你能把表和你最终的语句,发上来吗 ? 如果不涉密的话。放在下载里,我下载下来看看。
经实验,列数和行数只有几个的表不报错,但是行数列数多些的表都报这个错,表内容类型有汉字,整型数值,浮点数值,日期,经纬度。
joancq 2015-03-10
  • 打赏
  • 举报
回复
代码放下载了,名字是给wmxcn2000,谢谢帮忙!
joancq 2015-03-10
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
你能把表和你最终的语句,发上来吗 ? 如果不涉密的话。放在下载里,我下载下来看看。


joancq 2015-03-09
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:
-- 给你一个现成的,表名传入时,要用大写。

SQL> create directory xmlfiledir as 'c:\' ;

目录已创建。

SQL> create user test identified by test default tablespace users ;

用户已创建。

SQL> grant connect , resource , dba to test ;

授权成功。

SQL> grant read , write on directory xmlfiledir to test ;

授权成功。

SQL> conn test/test
已连接。

SQL> create table x as select * from dba_objects where rownum<10;

表已创建。
SQL> create or replace procedure gen_xml_bytname(p_tname varchar2)
  2  as
  3    type type_cur is ref cursor;
  4    cur       type_cur;
  5    m_strsql  varchar2(2000);
  6    m_rowinfo varchar2(2000);
  7    hfile Utl_File.file_type  ;
  8    dwCount int := 0 ;
  9  begin
 10    m_strsql := 'select ';
 11    for x in (select cname from col where tname = p_tname) loop
 12      m_strsql := m_strsql || '''<' || x.cname || '>'' || ' || x.cname || ' |
| ''</' || x.cname || '>'' || chr(10) ||' ;
 13    end loop;
 14    m_strsql := substr( m_strsql ,1,length(m_strsql) -2) || ' as c  from ' ||
 p_tname;
 15    dbms_output.put_line(m_strsql) ;
 16
 17    open cur for m_strsql;
 18    hfile := Utl_file.fopen('XMLFILEDIR',p_tname || '.xml','W') ;
 19    utl_file.put_line(hfile,'<?xml version="1.0" encoding="utf-8"?>  ');
 20    utl_file.put_line(hfile,'<' || p_tname  || '>');
 21    loop
 22      fetch cur into m_rowinfo;
 23      exit when cur%notfound;
 24      dwCount := dwCount + 1 ;
 25      utl_file.put_line(hfile,'<rowno>' || dwCount || '</rowno>');
 26      utl_file.put_line(hfile,m_rowinfo);
 27    end loop;
 28    utl_file.put_line(hfile,'<' || p_tname || '>');
 29    utl_file.fclose_all;
 30    close cur;
 31  end;
 32  /

过程已创建。

SQL> call gen_xml_bytname('X');

调用完成。

SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断
开

C:\Documents and Settings\Administrator>type c:\x.xml
<?xml version="1.0" encoding="utf-8"?>
<X>
<rowno>1</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>ICOL$</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>20</OBJECT_ID>
<DATA_OBJECT_ID>2</DATA_OBJECT_ID>
<OBJECT_TYPE>TABLE</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>1</NAMESPACE>
<EDITION_NAME></EDITION_NAME>
...................
...................
<rowno>9</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>I_CDEF2</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>54</OBJECT_ID>
<DATA_OBJECT_ID>54</DATA_OBJECT_ID>
<OBJECT_TYPE>INDEX</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>4</NAMESPACE>
<EDITION_NAME></EDITION_NAME>

<X>

C:\Documents and Settings\Administrator>
引用 3 楼 wmxcn2000 的回复:
-- 给你一个现成的,表名传入时,要用大写。

SQL> create directory xmlfiledir as 'c:\' ;

目录已创建。

SQL> create user test identified by test default tablespace users ;

用户已创建。

SQL> grant connect , resource , dba to test ;

授权成功。

SQL> grant read , write on directory xmlfiledir to test ;

授权成功。

SQL> conn test/test
已连接。

SQL> create table x as select * from dba_objects where rownum<10;

表已创建。
SQL> create or replace procedure gen_xml_bytname(p_tname varchar2)
  2  as
  3    type type_cur is ref cursor;
  4    cur       type_cur;
  5    m_strsql  varchar2(2000);
  6    m_rowinfo varchar2(2000);
  7    hfile Utl_File.file_type  ;
  8    dwCount int := 0 ;
  9  begin
 10    m_strsql := 'select ';
 11    for x in (select cname from col where tname = p_tname) loop
 12      m_strsql := m_strsql || '''<' || x.cname || '>'' || ' || x.cname || ' |
| ''</' || x.cname || '>'' || chr(10) ||' ;
 13    end loop;
 14    m_strsql := substr( m_strsql ,1,length(m_strsql) -2) || ' as c  from ' ||
 p_tname;
 15    dbms_output.put_line(m_strsql) ;
 16
 17    open cur for m_strsql;
 18    hfile := Utl_file.fopen('XMLFILEDIR',p_tname || '.xml','W') ;
 19    utl_file.put_line(hfile,'<?xml version="1.0" encoding="utf-8"?>  ');
 20    utl_file.put_line(hfile,'<' || p_tname  || '>');
 21    loop
 22      fetch cur into m_rowinfo;
 23      exit when cur%notfound;
 24      dwCount := dwCount + 1 ;
 25      utl_file.put_line(hfile,'<rowno>' || dwCount || '</rowno>');
 26      utl_file.put_line(hfile,m_rowinfo);
 27    end loop;
 28    utl_file.put_line(hfile,'<' || p_tname || '>');
 29    utl_file.fclose_all;
 30    close cur;
 31  end;
 32  /

过程已创建。

SQL> call gen_xml_bytname('X');

调用完成。

SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断
开

C:\Documents and Settings\Administrator>type c:\x.xml
<?xml version="1.0" encoding="utf-8"?>
<X>
<rowno>1</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>ICOL$</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>20</OBJECT_ID>
<DATA_OBJECT_ID>2</DATA_OBJECT_ID>
<OBJECT_TYPE>TABLE</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>1</NAMESPACE>
<EDITION_NAME></EDITION_NAME>
...................
...................
<rowno>9</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>I_CDEF2</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>54</OBJECT_ID>
<DATA_OBJECT_ID>54</DATA_OBJECT_ID>
<OBJECT_TYPE>INDEX</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>4</NAMESPACE>
<EDITION_NAME></EDITION_NAME>

<X>

C:\Documents and Settings\Administrator>
谢谢高手指点,试了一下,但是报ORA-06502和ORA-06512的错,忽略后可以继续执行成功,但是报错是哪里的问题啊,求解!
joancq 2015-02-10
  • 打赏
  • 举报
回复
引用 1 楼 Ranyoo 的回复:
创建一个dir,用动态sql拼游标,然后xmldom生成xml文件导出。大体上是这个思路。
谢谢,但是我对语句不太熟,能不能说详细点?感谢!
卖水果的net 2015-02-10
  • 打赏
  • 举报
回复
-- 给你一个现成的,表名传入时,要用大写。

SQL> create directory xmlfiledir as 'c:\' ;

目录已创建。

SQL> create user test identified by test default tablespace users ;

用户已创建。

SQL> grant connect , resource , dba to test ;

授权成功。

SQL> grant read , write on directory xmlfiledir to test ;

授权成功。

SQL> conn test/test
已连接。

SQL> create table x as select * from dba_objects where rownum<10;

表已创建。
SQL> create or replace procedure gen_xml_bytname(p_tname varchar2)
  2  as
  3    type type_cur is ref cursor;
  4    cur       type_cur;
  5    m_strsql  varchar2(2000);
  6    m_rowinfo varchar2(2000);
  7    hfile Utl_File.file_type  ;
  8    dwCount int := 0 ;
  9  begin
 10    m_strsql := 'select ';
 11    for x in (select cname from col where tname = p_tname) loop
 12      m_strsql := m_strsql || '''<' || x.cname || '>'' || ' || x.cname || ' |
| ''</' || x.cname || '>'' || chr(10) ||' ;
 13    end loop;
 14    m_strsql := substr( m_strsql ,1,length(m_strsql) -2) || ' as c  from ' ||
 p_tname;
 15    dbms_output.put_line(m_strsql) ;
 16
 17    open cur for m_strsql;
 18    hfile := Utl_file.fopen('XMLFILEDIR',p_tname || '.xml','W') ;
 19    utl_file.put_line(hfile,'<?xml version="1.0" encoding="utf-8"?>  ');
 20    utl_file.put_line(hfile,'<' || p_tname  || '>');
 21    loop
 22      fetch cur into m_rowinfo;
 23      exit when cur%notfound;
 24      dwCount := dwCount + 1 ;
 25      utl_file.put_line(hfile,'<rowno>' || dwCount || '</rowno>');
 26      utl_file.put_line(hfile,m_rowinfo);
 27    end loop;
 28    utl_file.put_line(hfile,'<' || p_tname || '>');
 29    utl_file.fclose_all;
 30    close cur;
 31  end;
 32  /

过程已创建。

SQL> call gen_xml_bytname('X');

调用完成。

SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断
开

C:\Documents and Settings\Administrator>type c:\x.xml
<?xml version="1.0" encoding="utf-8"?>
<X>
<rowno>1</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>ICOL$</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>20</OBJECT_ID>
<DATA_OBJECT_ID>2</DATA_OBJECT_ID>
<OBJECT_TYPE>TABLE</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>1</NAMESPACE>
<EDITION_NAME></EDITION_NAME>
...................
...................
<rowno>9</rowno>
<OWNER>SYS</OWNER>
<OBJECT_NAME>I_CDEF2</OBJECT_NAME>
<SUBOBJECT_NAME></SUBOBJECT_NAME>
<OBJECT_ID>54</OBJECT_ID>
<DATA_OBJECT_ID>54</DATA_OBJECT_ID>
<OBJECT_TYPE>INDEX</OBJECT_TYPE>
<CREATED>02-4月 -10</CREATED>
<LAST_DDL_TIME>02-4月 -10</LAST_DDL_TIME>
<TIMESTAMP>2010-04-02:13:18:38</TIMESTAMP>
<STATUS>VALID</STATUS>
<TEMPORARY>N</TEMPORARY>
<GENERATED>N</GENERATED>
<SECONDARY>N</SECONDARY>
<NAMESPACE>4</NAMESPACE>
<EDITION_NAME></EDITION_NAME>

<X>

C:\Documents and Settings\Administrator>
singsongs 2015-02-09
  • 打赏
  • 举报
回复
创建一个dir,用动态sql拼游标,然后xmldom生成xml文件导出。大体上是这个思路。

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧