SQL> /* Creating Your table */
SQL> CREATE TABLE IBSCOLYTD
2 (
3 ACTNOI VARCHAR2 (8),
4 MEMONOI NUMBER (7,0),
5 MEMODTEI DATE,
6 AMOUNTI NUMBER (8,0),
7 BRCDSI NUMBER (4,0),
8 TYPEI NUMBER (4,0),
9 TRANSMONI NUMBER (6,0)
10 );
My XML file is:SQL> CREATE TABLE IBSCOLYTD
2 (
3 ACTNOI VARCHAR2 (8),
4 MEMONOI NUMBER (7,0),
5 MEMODTEI DATE,
6 AMOUNTI NUMBER (8,0),
7 BRCDSI NUMBER (4,0),
8 TYPEI NUMBER (4,0),
9 TRANSMONI NUMBER (6,0)
10 );
<?xml version="1.0"?>
<ROWSET>
<IBSCOLYTD>
<ACTNOI>28004125</ACTNOI>
<MEMONOI>251942</MEMONOI>
<MEMODTEI>05-SEP-92</MEMODTEI>
<AMOUNTI>400</AMOUNTI>
<BRCDSI>513</BRCDSI>
<TYPEI>1</TYPEI>
<TRANSMONI>0</TRANSMONI>
</IBSCOLYTD>
<IBSCOLYTD>
<ACTNOI>28004125</ACTNOI>
<MEMONOI>251943</MEMONOI>
<MEMODTEI>04-OCT-92</MEMODTEI>
<AMOUNTI>400</AMOUNTI>
<BRCDSI>513</BRCDSI>
<TYPEI>1</TYPEI>
<TRANSMONI>0</TRANSMONI>
</IBSCOLYTD>
</ROWSET>
My goal is to load the values from the XML file into the respective
columns of the table.Now the actual code
SQL> CREATE OR REPLACE PROCEDURE insert_xml_emps(p_directory in varchar2,
2 p_filename in varchar2,
3 vtableName in varchar2) as
4 v_filelocator BFILE;
5 v_cloblocator CLOB;
6 l_ctx DBMS_XMLSTORE.CTXTYPE;
7 l_rows NUMBER;
8 v_amount_to_load NUMBER;
9 dest_offset NUMBER := 1;
10 src_offset NUMBER := 1;
11 lang_context NUMBER := DBMS_LOB.DEFAULT_LANG_CTX;
12 warning NUMBER;
13 BEGIN
14 dbms_lob.createtemporary(v_cloblocator, true);
15 v_filelocator := bfilename(p_directory, p_filename);
16 dbms_lob.open(v_filelocator, dbms_lob.file_readonly);
17 v_amount_to_load := DBMS_LOB.getlength(v_filelocator);
18 --- ***This line is changed*** ---
19 DBMS_LOB.LOADCLOBFROMFILE(v_cloblocator,
20 v_filelocator,
21 v_amount_to_load,
22 dest_offset,
23 src_offset,
24 0,
25 lang_context,
26 warning);
27
28 l_ctx := DBMS_XMLSTORE.newContext(vTableName);
29 DBMS_XMLSTORE.setRowTag(l_ctx, 'ROWSET');
30 DBMS_XMLSTORE.setRowTag(l_ctx, 'IBSCOLYTD');
31 -- clear the update settings
32 DBMS_XMLStore.clearUpdateColumnList(l_ctx);
33 -- set the columns to be updated as a list of values
34 DBMS_XMLStore.setUpdateColumn(l_ctx, 'ACTNOI');
35 DBMS_XMLStore.setUpdateColumn(l_ctx, 'MEMONOI');
36 DBMS_XMLStore.setUpdatecolumn(l_ctx, 'MEMODTEI');
37 DBMS_XMLStore.setUpdatecolumn(l_ctx, 'AMOUNTI');
38 DBMS_XMLStore.setUpdatecolumn(l_ctx, 'BRCDSI');
39 DBMS_XMLStore.setUpdatecolumn(l_ctx, 'TYPEI');
40 DBMS_XMLStore.setUpdatecolumn(l_ctx, 'TRANSMONI');
41 -- Now insert the doc.
42 l_rows := DBMS_XMLSTORE.insertxml(l_ctx, v_cloblocator);
43 DBMS_XMLSTORE.closeContext(l_ctx);
44 dbms_output.put_line(l_rows || ' rows inserted...');
45 dbms_lob.close(v_filelocator);
46 DBMS_LOB.FREETEMPORARY(v_cloblocator);
47 END;
48 /
Procedure created.
SQL> BEGIN
2 insert_xml_emps('TEST_DIR','load.xml','IBSCOLYTD');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM ibscolytd;
ACTNOI MEMONOI MEMODTEI AMOUNTI BRCDSI TYPEI TRANSMONI
-------- ---------- --------- ---------- ---------- ---------- ----------
28004125 251942 05-SEP-92 400 513 1 0
28004125 251943 04-OCT-92 400 513 1 0
SQL>
The same code is published on OTN by me.