您的位置:首页 > 其它

xml与oralce的导入导出

2009-03-09 17:56 357 查看
直接在sql中运行:

insert:

declare
2 l_sales_emp xmltype;
3 l_ctx dbms_xmlstore.ctxtype;
4 l_rows pls_integer;
5 begin
6 -- get all the sales employees into an xml document
7 select xmlelement("ROWSET",
8 xmlagg(
9 xmlelement("ROW",
10 xmlforest(e.empno,e.ename,e.job,e.mgr,
11 e.hiredate,e.sal,e.comm,e.deptno)
12 )
13 )
14 )
15 into l_sales_emp
16 from emp e, dept d
17 where d.deptno = e.deptno
18 and d.dname='SALES';
19
20 -- setup our dbms_xmlstore context
21 l_ctx := dbms_xmlstore.newcontext('SALES_EMP');
22 l_rows := dbms_xmlstore.insertxml(
23 L_ctx, l_sales_emp.getClobVal());
24
25 -- how many rows were inserted?
26 dbms_output.put_line(l_rows || ' rows inserted into SALES_EMP.');
27
28 -- clean up
29 dbms_xmlstore.closecontext(l_ctx);
30 end;

update:

declare
2 l_sales_emp xmltype;
3 l_ctx dbms_xmlstore.ctxtype;
4 l_rows pls_integer;
5 begin
6 -- simulate the updates to make
7 l_sales_emp := xmltype('<?xml version="1.0"?>
8 <ROWSET>
9 <ROW>
10 <EMPNO>7499</EMPNO><ENAME>ALLEN</ENAME><JOB>MANAGER</JOB>
<MGR>7698</MGR><SAL>2600</SAL>
11 </ROW>
12 <ROW>
13 <EMPNO>7521</EMPNO><ENAME>WARD</ENAME><JOB>MANAGER</JOB>
<MGR>7698</MGR><SAL>2250</SAL>
14 </ROW>
15 </ROWSET>');
16
17 -- setup our dbms_xmlstore context
18 l_ctx := dbms_xmlstore.newcontext('SALES_EMP');
19
20 -- setup the columns to be updated
21 dbms_xmlstore.clearupdatecolumnlist(l_ctx);
22 dbms_xmlstore.setupdatecolumn(l_ctx, 'ENAME');
23 dbms_xmlstore.setupdatecolumn(l_ctx, 'JOB');
24 dbms_xmlstore.setupdatecolumn(l_ctx, 'MGR');
25 dbms_xmlstore.setupdatecolumn(l_ctx, 'SAL');
26
27 -- setup the key columns to update by
28 dbms_xmlstore.setkeycolumn(l_ctx, 'EMPNO');
29
30 -- perform the update
31 l_rows := dbms_xmlstore.updatexml(l_ctx, l_sales_emp.getClobVal());
32
33 -- how many rows were updated?
34 dbms_output.put_line(l_rows || ' rows updated in SALES_EMP.');
35
36 -- clean up
37 dbms_xmlstore.closecontext(l_ctx);
38 end;

delete:

declare
2 l_sales_emp xmltype;
3 l_ctx dbms_xmlstore.ctxtype;
4 l_rows pls_integer;
5 begin
6 -- simulate the updates to make
7 l_sales_emp := xmltype('<?xml version="1.0"?>
8 <ROWSET>
9 <ROW>
10 <EMPNO>7499</EMPNO>
11 </ROW>
12 <ROW>
13 <EMPNO>7698</EMPNO>
14 </ROW>
15 </ROWSET>');
16
17 -- setup our dbms_xmlstore context
18 l_ctx := dbms_xmlstore.newcontext('SALES_EMP');
19
20 -- setup the key column to delete by
21 dbms_xmlstore.setkeycolumn(l_ctx, 'EMPNO');
22
23 -- perform the delete
24 l_rows := dbms_xmlstore.deletexml(l_ctx, l_sales_emp.getClobVal());
25
26 -- how many rows were deleted?
27 dbms_output.put_line(l_rows || ' rows deleted fm SALES_EMP.');
28
29 -- clean up
30 dbms_xmlstore.closecontext(l_ctx);
31 end;

生成xml文件

SELECT xmlgen.getXml(
'SELECT empno "EMP_NO"
, ename "NAME"
, deptno "DEPT_NO"
FROM emp
WHERE deptno = 10'
, 0
) FROM dual;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: