?
Solved

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

Posted on 2003-11-04
5
Medium Priority
?
1,706 Views
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"?>
<root>
      <Record TableID="1" TableField="Field1" Value=""/>
      <Record TableID="2" TableField="Field2" Value=""/>
      <Record TableID="3" TableField="Field3" Value=""/>
</root>

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

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.
0
Comment
Question by:RRR
[X]
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
5 Comments
 
LVL 3

Author Comment

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

Thanks,
RRR.
0
 
LVL 50

Expert Comment

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

Accepted Solution

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

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

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,
PBATCH_SIZE IN PLS_INTEGER DEFAULT 7777)
is    
insCtx DBMS_XMLSave.ctxType;    
rows number;  
begin    
insCtx := DBMS_XMLSave.newContext(tableName); -- get the context handle
   DBMS_XMLSave.Setbatchsize(insCtx,PBATCH_SIZE);
   DBMS_XMLSave.Setrowtag(insCtx,XMLROWS_TAG);
rows := DBMS_XMLSave.insertXML(insCtx,xmlDoc); -- this inserts the

document    
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
                            is
         l_ctx    dbms_xmlquery.ctxType;
         l_xml    clob;
       begin
         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 ) );

         dbms_xmlquery.closeContext(l_ctx);
         return l_xml;
       exception
         when others then
         dbms_xmlquery.closeContext(l_ctx);
       end xml_dataF;
and finaly the getting proc:
create or replace procedure fetch_xml_val_pF
 is
p_xml CLOB;
REC_XML clob;
         l_xml  varchar2(30000);
         l_line varchar2(2000);
         l_amt  pls_integer := 30000;
         l_idx  pls_integer := 1;
       begin
  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);
                     loop
           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:
ALSO CAN BE USED WITH CLOB PARAMETERS
declare
v_xml clob:='<?xml version="1.0" encoding="UTF-8"?>
<root>
     <Record TableID="1" TableField="Field1" Value=""/>
     <Record TableID="2" TableField="Field2" Value=""/>
     <Record TableID="3" TableField="Field3" Value=""/>
</root>
';
begin
-- select sys.xmldom.(m.col1) into v_xml from metadata m;
  -- Call the procedure
  fetch_xml_val(p_xml => v_xml,
                 ptab=>'META_TABS');
end;

0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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