当前位置:编程学习 > XML/UML >>

oracle存储过程生成xml文件

oracle存储过程生成xml文件
 
CREATE OR REPLACE PROCEDURE Pro_OracleToXML(personid varchar2,name varchar2,address varchar2,tel varchar2,ip varchar2,email varchar2)
AS
   isql varchar2(200);--创建临时表
   dptable varchar2(100);--删除临时表
   i_insert varchar2(200);--将数据插入临时表
   tableSource CLOB;
   str varchar2(500);
   xmlFile utl_file.file_type;
   tempsql varchar2(500) ;       --初始的查询语句
   ex    BOOLEAN;--文件是否存在
   flen NUMBER;--文件长度?
   bsize NUMBER;--文件大小
 BEGIN
    --初始化创建临时表语句
    isql:='create global temporary table  people_copy(personid VARCHAR2(4),name varchar2(50),address VARCHAR2(200),tel VARCHAR2(20),fax VARCHAR2(20),email VARCHAR2(100)) on commit delete rows';
    --创建临时表
    execute immediate isql; 
    dbms_output.put_line(isql||'执行成功');
    --将触发后的数据插入到people_copy表中
    i_insert := 'insert into people_copy values('''||personid||''','''||name||''','''||address||''','''||tel||''','''||ip||''','''||email||''')';
    --执行插入语句
    execute immediate i_insert;
    --将临时表的查询语句作为值赋给tempsql变量
    tempsql := 'SELECT * FROM people_copy where fax= '''||ip||''' order by personid asc';
    dbms_output.put_line(tempsql);
    --获得内容
    tableSource:=dbms_xmlgen.getXml(tempsql);
    --判断文件是否存在
    utl_file.fgetattr('PEOPLE_FILE_DIR','/'||ip||'.xml', ex,flen,bsize); 
    --chr(10)是换行符,
    --chr(13)是回车,
    --replace(replace(tableSource,CHR(10),''),chr(13),'');
    if ex then
      --文件存在,将tableSource的值的<?xml version="1.0"?>替换为空格
      tableSource:=replace(tableSource,'<?xml version="1.0"?>','');
    else
      --文件不存在,不用替换
      dbms_output.put_line('File Does Not Exist');
    end if;
    --打开文件
    xmlFile:=utl_file.fopen('PEOPLE_FILE_DIR','/'||ip||'.xml','A');
    --将tableSource的内容赋给str字符串变量
    str := tableSource||'';
    --去除str前面的空格
    tableSource := trim(leading CHR(10) from str);
    --去除tableSource后面的空格
    tableSource := trim(trailing CHR(10) from tableSource);
    dbms_output.put_line(tableSource);
    --输入tableSource内容到xml文件中
    utl_file.put_line(xmlFile,tableSource);
    --关闭文件
    utl_file.fclose(xmlFile);
    --将删除临时表的语句作为值赋给dptable变量
    dptable :='drop table people_copy';
    --删除临时表
    execute immediate dptable; 
  --出现异常,输出异常信息   
  EXCEPTION
    WHEN OTHERS THEN
      dbms_output.put_line(SQLERRM);
 END Pro_OracleToXML;
 
 
create or replace trigger trigger_people
after insert or update on people
referencing
for each row
  
declare
PRAGMA AUTONOMOUS_TRANSACTION;
begin
  dbms_output.put_line(:new.personid||'已经触发了!---');
  Pro_OracleToXML(:new.personid,:new.name,:new.address,:new.tel,:new.fax,:new.email);
end;
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,