create or replace procedure up_exptoxml(XMLfilepath in varchar2, sqlQuery in varchar2) is
sqlstr_from_file VARCHAR2(200);--保存sql查询字符串
document xmldom.DOMDocument;--文档对象
rootelement xmldom.DOMElement;--根元素
stockRecord xmldom.DOMElement;--记录
stockfield xmldom.DOMElement;--字段
l_cursor integer;--存储游标变量返回值
l_col_cot integer;--游标所包含的列数
l_desc_tab dbms_sql.desc_tab;--定义desc_tab类型的集合
tempfield dbms_sql.Varchar2_Table;--定义Varchar2_Table类型的集合
tempdate dbms_sql.Date_Table;--定义Date_Table类型的集合
tempnumber dbms_sql.Number_Table;--定义Number_Table类型的集合
tempnumber1 dbms_sql.Number_Table;
tempnode xmldom.DOMNode;--定义临时节点
text xmldom.DOMText;--定义文本节点
executenum integer;--存储游标执行返回值
--fieldvalue varchar2(50);
sqlfile utl_file.file_type;
parameter exception;--自定义异常
begin
dbms_output.put_line('hah:'||sqlstr_from_file);
--if upper(flag)<>'SQL'and upper(flag)<>'FILE' then
--raise parameter;--如果flag参数不为上述两个选项,则抛出参数异常
--elsif upper(flag)='SQL' then
sqlstr_from_file:=sqlQuery;
dbms_output.put_line('hah:'||sqlstr_from_file);
--else
--调用存储过程以读取文件中存放的sql查询字符串
--readFile(sqlQuery,sqlstr_from_file);
--end if;l_cursor:=dbms_sql.open_cursor;
dbms_output.put_line('hah:'||sqlstr_from_file);
dbms_sql.parse(l_cursor,sqlstr_from_file,dbms_sql.native);--解析游标
dbms_output.put_line('hah:解析游标'||sqlstr_from_file||l_cursor);
dbms_sql.describe_columns(l_cursor,l_col_cot,l_desc_tab);--获取列描述信息
dbms_output.put_line('hah:获取列描述信息'||sqlstr_from_file||l_col_cot);
for def in 1..l_col_cot loop
dbms_output.put_line('hah:'||sqlstr_from_file||l_col_cot);
if l_desc_tab(def).col_type=1 then
tempfield(def):='';--初始化集合元素
dbms_output.put_line('hah:初始化集合元素'||sqlstr_from_file);
dbms_sql.define_column(l_cursor,def,tempfield(def),50);--定义列
dbms_output.put_line('hah:-定义列'||sqlstr_from_file||tempfield(def));
elsif l_desc_tab(def).col_type=2 thentempnumber(def):=0;--初始化集合元素
dbms_output.put_line('hah:初始化集合元素'||sqlstr_from_file);
dbms_sql.define_column(l_cursor,def,tempnumber(def));--定义列
 
elsetempnumber1(def):=0;--初始化集合元素
 
dbms_sql.define_column(l_cursor,def,tempnumber1(def));--定义列end if;
end loop;executenum:=dbms_sql.execute(l_cursor);--执行游标
 
document:=xmldom.newDOMDocument;--生成文档对象
 
rootelement:=xmldom.createElement(document,'StockTable');--生成根元素
 
loop--循环结果集(列方向)
if dbms_sql.fetch_rows(l_cursor)>0 then
stockrecord:=xmldom.createElement(document,'Stock');--建立临时记录元素
 
for i in 1..l_col_cot loop--(循环列,行方向)stockfield:=xmldom.createElement(document,l_desc_tab(i).col_name);--建立字段元素
dbms_output.put_line('hah:建立字段元素'||sqlstr_from_file);
if l_desc_tab(i).col_type=1 then--col_type=1 表示varchar2类型
--dbms_sql.define_column(l_cursor,i,tempfield(i),50);
dbms_sql.column_value(l_cursor,i,tempfield(i));
text:=xmldom.createTextNode(document,tempfield(i));
dbms_output.put_line('hah:表示varchar2类型'||sqlstr_from_file);
else --col_type=12 表示date类型
--dbms_sql.define_column(l_cursor,i,tempdate(i));
dbms_sql.column_value(l_cursor,i,tempdate(i));
dbms_output.put_line('hah: 表示date类型'||sqlstr_from_file);
text:=xmldom.createTextNode(document,to_char(tempdate(i),'yyyy-mm-dd HH24:MI:SS'));
end if;dbms_output.put_line('hah:'||sqlstr_from_file);--将文本节点添加到字段节点
tempnode:=xmldom.appendChild(xmldom.makeNode(stockfield),xmldom.makeNode(text));
--将字段节点添加到记录节点
tempnode:=xmldom.appendChild(xmldom.makeNode(stockrecord),xmldom.makeNode(stockfield));
end loop;
--将记录节点添加到根节点
tempnode:=xmldom.appendChild(xmldom.makeNode(rootelement),xmldom.makeNode(stockrecord));
dbms_output.put_line('hah:'||sqlstr_from_file);
else
--no more row to copy
dbms_sql.close_cursor(l_cursor);--遍历游标结束,关闭游标dbms_output.put_line('hah:'||sqlstr_from_file);
exit;
end if;
end loop;
--将根节点添加到文档
dbms_output.put_line('hah:'||sqlstr_from_file);
tempnode:=xmldom.appendChild(xmldom.makeNode(document),xmldom.makeNode(rootelement));
xmldom.setVersion(document,'1.0');
xmldom.writeToFile(document,XMLfilepath);
exception 
when parameter then
raise_application_error(-20004,'必须以"sql"或者"file"方式指定sql语句的来源');
when others then
if dbms_sql.is_open(l_cursor) then
dbms_sql.close_cursor(l_cursor);
end if;
raise_application_error(-20006,'hahaha');
--raise_application_error(-20005,'未知异常,游标已关闭!');
end up_exptoxml;
-------------------------------------------------------------------本人写了一道代码如上,现在发生异常
错误如下,不知道有哪个高手呢解决呢?给高分30
急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
----------------------ORA-20006: hahaha
ORA-06512: 在 "TEST1.UP_EXPTOXML", line 116
ORA-06512: 在 line 1