PL SQL example for procedure to get XML, Update it and return.

Posted on 2003-11-04
Medium Priority
Last Modified: 2012-06-27
Hi, Experts.
I need to write some procedure that will receive XML, analyze/parse it (take the Table Name from my MetaData table by using TableID that XML give me ) and return the Value of this table by using the code and Table name (Update the Value attribute in the incomming XML).

I have a MetaData Table like this:
TableID    |  TableName   |   TableField   | Value  |
1             |  Table1          |   Field1         | 1000   |
1             |  Table1          |   Field8         | 2345   |
2             |  Table2          |   Field2         | 2000   |
3             |  Table3          |   Field3         | 3000   |
3             |  Table12        |   Field2         | 23123  |

The TableID and TableField fields are PK for me.

The lookup table named Table1 looks like this:
Field1   |  Field2  | ... |  Field8  |
1000    | 1236     | ... |  2345    |

The in comming XML Example is:
<?xml version="1.0" encoding="UTF-8"?>
      <Record TableID="1" TableField="Field1" Value=""/>
      <Record TableID="2" TableField="Field2" Value=""/>
      <Record TableID="3" TableField="Field3" Value=""/>

The Returned XML should be like this:
The in comming XML Example is:
<?xml version="1.0" encoding="UTF-8"?>
      <Record TableID="1" TableField="Field1" Value="1000"/>
      <Record TableID="2" TableField="Field2" Value="2000"/>
      <Record TableID="3" TableField="Field3" Value="3000"/>

I am just selecting the Value=1000 from Table1 and Field1 and Update the XML.
Please, help me.
I need the exact PL SQL select statement to do this.

This question is important me and very urgent!!!
(500 Points - It wiil be graded as the Best = 500 x4)
Thanks, RRR.
Question by:RRR
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 9679651
Some correction:
The Value field in not exists In MetaData table.

LVL 50

Expert Comment

ID: 9680456
you'll probably be better off asking this in the ORACLE database Topic Area,
LVL 21

Accepted Solution

oleggold earned 2000 total points
ID: 10605711
You can try something like this:

1. test case for Your tables:
  for i in 1..10
 insert into meta_tabs
 (tableid, tablename, tablefield, value)
  (i, 'TABLE'||i, 'FIELD'||i,i+i*1000);
  end loop;

You can use an intermediate table to store Your XML
create table MetaData (col1 xmltype);
then use the following procedure to insert Your XML:

procedure InsXML(xmlDoc IN CLOB, tableName IN VARCHAR2,
insCtx DBMS_XMLSave.ctxType;    
rows number;  
insCtx := DBMS_XMLSave.newContext(tableName); -- get the context handle
rows := DBMS_XMLSave.insertXML(insCtx,xmlDoc); -- this inserts the

dbms_output.put_line(to_char(rows) || ' rows inserted');
writelog(000, to_char(rows) || ' rows inserted');  
DBMS_XMLSave.closeContext(insCtx);            -- this closes the handle
end InsXML;

then the following function will be used from

fetch_xml_val_F  to parse the values and append fields values to :
CREATE OR REPLACE FUNCTION xml_dataF( p_sql         in varchar2,
                           p_max_rows in number )
               RETURN CLOB
         l_ctx    dbms_xmlquery.ctxType;
         l_xml    clob;
         dbms_lob.createtemporary( l_xml, true, dbms_lob.session );
         l_ctx := dbms_xmlquery.newContext( p_sql );

         -- setup the context
         dbms_xmlquery.setRaiseNoRowsException( l_ctx,true );
         dbms_xmlquery.setMaxRows( l_ctx, p_max_rows );

         -- get the xml fm the context
         l_xml := dbms_xmlquery.getXML( l_ctx );

         -- print out the xml result set
       --  print_xml( l_xml );

         -- determine the no of rows processed
         dbms_output.put_line( '' );
         dbms_output.put_line( 'No. of rows processed: ' ||
                               dbms_xmlquery.getnumrowsprocessed( l_ctx ) );

         return l_xml;
         when others then
       end xml_dataF;
and finaly the getting proc:
create or replace procedure fetch_xml_val_pF
p_xml CLOB;
REC_XML clob;
         l_xml  varchar2(30000);
         l_line varchar2(2000);
         l_amt  pls_integer := 30000;
         l_idx  pls_integer := 1;
  select t.col1.extract('//root/Record/TableID/text()text()') || ',' ||
  t.col1.extract('//root/Record TableID/text()')
  into  p_xml
 from METADATA t;  
 REC_XML:=xml_dataF(p_sql => 'select value from meta_tabs',p_max_rows => l_idx);
         for i in 1 .. ceil(dbms_lob.getlength(p_xml)/l_amt) loop
           l_xml := dbms_lob.substr(p_xml, l_amt, l_idx);
           exit when l_xml is null;
   l_line := substr(l_xml, 1, instr(l_xml, chr(10))-1);
   dbms_output.put_line('|' || l_line);
          l_xml := substr(l_xml, instr(l_xml, chr(10)||REC_XML)+1);

           end loop;
         end loop;
       end ;

the XML output:
v_xml clob:='<?xml version="1.0" encoding="UTF-8"?>
     <Record TableID="1" TableField="Field1" Value=""/>
     <Record TableID="2" TableField="Field2" Value=""/>
     <Record TableID="3" TableField="Field3" Value=""/>
-- select sys.xmldom.(m.col1) into v_xml from metadata m;
  -- Call the procedure
  fetch_xml_val(p_xml => v_xml,


Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question