oracle中xml如何存储过程,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);--創(chuàng)建臨時表
dptable varchar2(100);--刪除臨時表
i_insert varchar2(200);--將數(shù)據(jù)插入臨時表
tableSource CLOB;
str varchar2(500);
xmlFile utl_file.file_type;
tempsql varchar2(500) ; ? ? ? --初始的查詢語句
ex ? ?BOOLEAN;--文件是否存在
flen NUMBER;--文件長度?
bsize NUMBER;--文件大小
BEGIN
--初始化創(chuàng)建臨時表語句
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';
--創(chuàng)建臨時表
execute immediate isql;
dbms_output.put_line(isql||'執(zhí)行成功');
--將觸發(fā)后的數(shù)據(jù)插入到people_copy表中
i_insert := 'insert into people_copy values('''||personid||''','''||name||''','''||address||''','''||tel||''','''||ip||''','''||email||''')';
--執(zhí)行插入語句
execute immediate i_insert;
--將臨時表的查詢語句作為值賦給tempsql變量
tempsql := 'SELECT * FROM people_copy where fax= '''||ip||''' order by personid asc';
dbms_output.put_line(tempsql);
--獲得內(nèi)容
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的內(nèi)容賦給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內(nèi)容到xml文件中
utl_file.put_line(xmlFile,tableSource);
--關(guān)閉文件
utl_file.fclose(xmlFile);
--將刪除臨時表的語句作為值賦給dptable變量
dptable :='drop table people_copy';
--刪除臨時表
execute immediate dptable;
--出現(xiàn)異常,輸出異常信息
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||'已經(jīng)觸發(fā)了!---');
Pro_OracleToXML(:new.personid,:new.name,:new.address,:new.tel,:new.fax,:new.email);
end;
總結(jié)
以上是生活随笔為你收集整理的oracle中xml如何存储过程,oracle存储过程生成xml文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (JAVA)格式化输出日期
- 下一篇: Go语言基础编程题