XML from the interlaced lines of two Tables?

I'm trying to generate XML data from interlaced lines of two relational tables that have a common FK, and I can't have enough to get the concept / syntax right. for example:
CREATE TABLE A (
  PK_A NUMBER ,
  FK     NUMBER ,
  C       VARCHAR2(100) ,
  D       VARCHAR2(100) 
) ;

CREATE TABLE B (
  PK_B NUMBER ,
  FK     NUMBER ,
  E       VARCHAR2(100) ,
  F       VARCHAR2(100) 
) ;

... load some data into both tables so B always has one more row than A for the any FK ...

  FUNCTION Get_XML(
    FK_In IN a.fk%TYPE
  ) RETURN XMLType
  IS
    TYPE A_Table_Type IS TABLE OF a%ROWTYPE ;
    TYPE B_Table_Type IS TABLE OF b%ROWTYPE ;
    TYPE XML_Table_Type IS TABLE OF XMLType INDEX BY PLS_INTEGER ;
    my_A_Table A_Table_Type ;
    my_B_Table B_Table_Type ;
    my_XML_Table XML_Table_Type ;
    my_Return_XML XMLType ;
    t NUMBER := 0 ;  -- my_A_Table index
    x PLS_INTEGER := 1 ;  -- my_XML_Table index
  BEGIN
    SELECT * BULK COLLECT INTO my_A_Table FROM a WHERE ( fk = FK_In ) ;
    SELECT * BULK COLLECT INTO my_B_Table FROM b WHERE ( fk = FK_In ) ;
    FOR i IN 1..my_B_Table.COUNT LOOP
      SELECT XMLElement( "B" , XMLAttributes( my_B_Table(i).E AS "ID" ) )
        INTO my_XML_Table( x ) FROM dual ;
      x := x + 1 ;
      IF ( t < my_B_Table.COUNT ) THEN
        t := t + 1 ;
        SELECT XMLElement( "A" , XMLAttributes( my_A_Table(t).C AS "ID" ) )
          INTO my_XML_Table( x ) FROM dual ;
        x := x + 1 ;
      END IF ;
    END LOOP ;
    SELECT XMLAgg( SELECT * FROM TABLE( my_XML_Table ) INTO my_Return_XML FROM dual ;
    RETURN my_Return_XML ;
  END Get_XML ;
-----

The statement "SELECT XMLAgg (... "does not, compile with one" ORA-00936: lack the expression "error." Using an associative array of XMLType is my last attempt to build up interlaced table A / B XML fragments in a single global XML fragment. At first, I just tried to XMLConcat each query results directly in my_Return_XML.

--
Alex

Hello

I see two problems here:

(1) lack of parentheses around SELECT it and closing XMLAgg:

SELECT XMLAgg( ( SELECT * FROM TABLE( my_XML_Table ) ) ) INTO my_Return_XML FROM dual ;

(2) you cannot use the TABLE function with a type declared for PL/SQL. You must create a SQL type and declare a collection of this type.
Alternatively, you can use SYS. XMLSEQUENCETYPE (varray of XMLType).

Anyway, is there a simpler approach?
For example, using a single SELECT statement:

SQL*Plus: Release 10.1.0.4.2 - Production on Mar. Mars 30 11:32:08 2010

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connecté à :
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> CREATE TABLE A (
  2    PK_A NUMBER ,
  3    FK     NUMBER ,
  4    C       VARCHAR2(100) ,
  5    D       VARCHAR2(100)
  6  ) ;

Table créée.

SQL> CREATE TABLE B (
  2    PK_B NUMBER ,
  3    FK     NUMBER ,
  4    E       VARCHAR2(100) ,
  5    F       VARCHAR2(100)
  6  ) ;

Table créée.

SQL> INSERT INTO a
  2  SELECT level, '1', 'A:VALUE '||to_char(level,'fm09'), 'A:VALUE '||to_char(level,'fm09')
  3  FROM dual
  4  CONNECT BY level <= 10;

10 ligne(s) créée(s).

SQL> INSERT INTO b
  2  SELECT level, '1', 'B:VALUE '||to_char(level,'fm09'), 'B:VALUE '||to_char(level,'fm09')
  3  FROM dual
  4  CONNECT BY level <= 11;

11 ligne(s) créée(s).

SQL> commit;

Validation effectuée.

SQL> set long 500
SQL> set pagesize 50
SQL> variable fk number;
SQL> exec :fk := 1;

Procédure PL/SQL terminée avec succès.

SQL> SELECT xmlagg(
  2           xmlelement(evalname(ename), xmlattributes(id)) order by rn
  3         ).extract('/*') --< for formatting purpose
  4         as xmldoc
  5  FROM (
  6    SELECT 2*rownum-1 as rn,
  7           'B'        as ename,
  8           e          as id
  9    FROM b
 10    WHERE fk = :fk
 11    UNION ALL
 12    SELECT 2*rownum,
 13           'A',
 14           c
 15    FROM a
 16    WHERE fk = :fk
 17  );

XMLDOC
--------------------------------------------------------------------------------





















Interlacing is made in the inner query by assigning same ROWNUMs odd and table A to table B.

Tags: Database

Similar Questions

  • [JDev12c, ADF] How to get the value of a field from the selected line in af:table and...

    Hallo,

    I want to double click on a line of an af:table to call a page that displays a form (based on a View object) with the details of the selected line.

    I need to go to the second page the value of a field on the line that is selected on the first page.

    How can I do this? In particular, how can I get the value of a field from the selected line? How can I call the second page on double-click on the af line: table?

    Thank you

    F.

    Why would user, you need to pass a value of the line to the shape?

    The framework selects the line you want to display in the form. All you have to do is to show the form with the selected line. It is the framework automatically as long as you use e vo even the same data control.

    Timo

    Post edited by: Timo Hahn
    And the handling double-clicks is described here http://www.oracle.com/technetwork/developer-tools/adf/learnmore/56-handle-doubleclick-in-table-170924.pdf

  • XML from the master-child tables

    Hello

    Just post here again for a quick response.

    XML from the master-child tables


    Best regards
    Hari

    Hello

    Using SQL/XML functions is the way to go in this case.

    SQL> select xmlroot(
      2           xmlelement("ROWSET",
      3             xmlagg(
      4               xmlelement("MASTER",
      5                 xmlforest(tm.master_id, tm.master_name, tm.master_location),
      6                   (
      7                    select xmlagg(
      8                             xmlelement("CHILD",
      9                               xmlforest(tc.child_id, tc.child_name, tc.child_location)
     10                             ) order by tc.child_id
     11                           )
     12                    from t_child tc
     13                    where tc.master_id = tm.master_id
     14                   )
     15               ) order by tm.master_id
     16             )
     17           )
     18         , version '1.0')
     19  from t_master tm
     20  where tm.master_id = 1
     21  ;
    
    XMLROOT(XMLELEMENT("ROWSET",XM
    --------------------------------------------------------------------------------
    
    
      
        1
        master name
        master location
        
          1
          child name 1
          child location 1
        
        
          2
          child name 2
          child location 2
        
        
          3
          child name 3
          child location 3
        
      
    
     
    

    Note that the first XMLAgg is optional here, since you want just a MASTER at a time.
    But if you need an XML document containing several masters, he will work as well.

    Alternatively, you can write a similar query with a GROUP BY clause (no correlated subquery, but a join between the master and the child instead):

    select xmlroot(
             xmlelement("ROWSET",
               xmlagg(
                 xmlelement("MASTER",
                   xmlforest(tm.master_id, tm.master_name, tm.master_location),
                     xmlagg(
                       xmlelement("CHILD",
                         xmlforest(tc.child_id, tc.child_name, tc.child_location)
                       ) order by tc.child_id
                     )
                 ) order by tm.master_id
               )
             )
           , version '1.0'
           )
    from t_master tm
         join t_child tc on tc.master_id = tm.master_id
    where tm.master_id = 1
    group by tm.master_id, tm.master_name, tm.master_location
    ;
    

    Published by: odie_63 on June 15, 2011 15:06

  • Error running webworks create from the command line in Beta 2 of Webworks

    Hello

    I am creating a project of cordova webworks from the command line. I run the following command:

    WebWorks create f:\devroot\NEXT_GEN\MobileApp\WebWorks2\MyApp com.joe.myApp MyApp

    However, it fails with the following error message:

    {[Error: check incorrect header] errno:-3, code: "Z_DATA_ERROR"}

    It seems to me that its attempts to download a tar.gzip drop of github (url below), but this file does not exist:

    URL: https://git-wip-us.apache.org/repos/asf?p=cordova-app-hello-world.git;a=snapshot;h=v2.0.0.54;sf=tgz

    So I'm not sure that my project directory is set up correctly. What is contained in this folder which its trying to find in the remote repository and is it relevant to my application?

    I can see my project directory in the space provided. It contains the following folders:

    ".cordova', 'merge', 'platforms', 'plugins' and 'www '.

    ".cordova" contains a subfolder 'hooks' which, in turn, contains about 24 cases being with 'after' or 'before' (e.g.after_build, before_build), it seems to be empty. It also contains a config.json that contains the attributes id and name of the project. Do any other necessary information in config.json?

    All other folders are empty.

    Is any other thing missing? should there be a config.xml file created for me in the directory "www" for example?

    Just add a config.xml file myself? Y at - it information about the correct structure for the config.xml file in webworks 2.0?

    Thank you

    Joe

    Hey Joe,

    So, just to confirm, HOST = "C:\". "and USERPROFILE ="C:\Users\?

    This is useful. Normally Windows machines do not have a HOME environment variable.

    We must ensure that these files copied to %USERPROFILE%\.cordova\lib\www\cordova\v2.0.0.54

    Please try this patch to webworks-cli/lib/utils/utils.js:

        findHomePath : function () {
            return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
        },
    
  • How to recover the second line of a table

    Hello

    Could someone let me know how to recover the second line of a table without using the coumn the names of columns in the table.

    By default the table EMP or used in the case of scott connection can be used as an example of table.


    Concerning
    select e.* from(
    select e.*,  rownum rn from emp e)e
    where e.rn =2
    

    did you expect that?

  • How to open a new tab from the address line?

    How to open a new tab from the address line?

    You will need to hold down the ALT key and press ENTER to open the link in a new tab without the use of an extension cord.
    By default the links always open in the same tab.

    You can search the Addons site for an extension if you want to change this default behavior.

  • How to reset Firefox from the command line (without a mouse) on Linux?

    I use Firefox for automated desktop screenshots xvfb virtual. There is no mouse. So I need to reset Firefox from the command line. Uninstall a re - install led again to the window "reset".
    I use Ubuntu Server Edition 13.04.

    Otherwise, without a keyboard and a mouse, you can just reinstall and delete. / mozilla folder.

  • Analyzer of sequence running from the command line in TestStand 2010

    Hello

    I would like to be able to run and control the stand-alone command line sequence Analyzer.  I can do that in 2014 TS (and it seems that I could patch versions as soon as 2012 TS to give them this ability), as described here: http://digital.ni.com/public.nsf/allkb/B927893F26BFB64A86257ECE006E442D

    However, I work with a large existing codebase in 2010 TS (with no intention to migrate to a newer version, in the foreseeable future).  Is it possible to get the same functionality from command line in the 2010 version of the parser?

    I know that I can open the command line parser, but are the commands, I am interested in: analyze, report, / save, /clearMessagesOnSave, / minimize, / help, / / leave.

    Thank you!

    Thanks Ryan, Jigg,

    Yes, I can run the parser (and opened a project), but basically, I can't run anything.

    I realized what I do by writing a TestStand sequence that calls the parser to sequence through its API (attached).  I can then run TestStand (and call this sequence) from the command line.

  • How to change Microsoft Security Essentials at the request of the parameters of scanning from the command line or in a script.

    Is it possible to change the parameters of the scheduled scan of the Microsoft Security Essentials from the command line or in a script?

    I know that I can use the command to schedule the MpCmdRun.exe tool command line to run, but that do not change or remove the existing scheduled scan that appears in the software. Run regedit to merge into the corresponding registry keys does not work because the keys are "locked", probably by the running of MSSE program.

    Suggest you post it in one of the forums dedicated to MSE - Microsoft Security Essentials: scanning, detection and removal of threats MowGreen Update Services - consumer safety

  • Add text from the command line to the output so that I can know how a command has been run when it generated the output

    This is a question and a suggestion...

    I would like to be able to precede or add if it works, the command line in the output file that creates the order.

    Example:

    I run dcdiag with switches, etc. and create an output file of results that gets saved or sent to a person or just put it in a folder. Of course, it could be that someone else ran the command and then sent the file. Anyway the problem is the same - what was the exact command line that has been executed to achieve these results?

    Question:

    Is there a known way to do from the command line that I can apply to any command?

    Suggestion:

    If this isn't the case, then I would suggest MS to include this function in all the BACK and PowerShell commands able to produce text output.

    If all goes well they monitor this forum and find it's a great idea and send me a check (pinky in my cheek) a meeellion of dollars. Then I and I alone will rule the world.

    Here you go:

    d:\>format i: / FS: NTFS > format.txt
    d:\>echo i format: / FS: NTFS > format.txt

  • Install the cartridge from the command line

    All,

    The problem, I am trying to solve right now is automated synchronization on all my servers cartridges.

    That said, is there a doc to install cartridges on a FMS from the command line? I don't see to be able to find anything.

    Thank you!

    It's the good FGLCMD for the installation of the cartridges. In case you want to install several cartridge I use this in addition to a find on unix systems command.

    Something like this code snippet (which I use in a recipe for checf

    It uses a MD5 checksum and verify changes in my repertoire of cartridge. If there are changes, it will run installs on all cartridges. You can change this option and check the individual by file md5 sums.

    script "install_additionalCartridges" do  interpreter "bash"  environment ("CarDir" => node.fglCache+"/"+node.fglVersion+"/cartridges")  user "foglight"  cwd "/foglight"  code <<-EOH
    
              md5sum $CarDir/*.car >/tmp/cc.md5
    
              diff /tmp/cc.md5 /foglight/state/cartridgesChef.md5 >/dev/null          if [ $? -ne 0 ]          then                    find $CarDir -name "*.car" -exec  /foglight/product/*/bin/fglcmd.sh -cmd cartridge:install -f "{}" ";"          fi
    
              md5sum $CarDir/*.car >/foglight/state/cartridgesChef.md5
    
      EOHend
    

    Make sure you add - usr and pwd - if you are not using the default settings and svr - if you do not want to do it remotely.

    Stefan

  • How to upgrade an ESXi host from the command line

    Sorry for bumping an old thread. I'm new to ESXi. I'm under ESXi 5.5 build 1331820 and I am at a loss how to apply patches.

    Preferably, I would like to apply all the relevant patches through console (SSH). Can someone give me pointers how to do this? Thank you

    Moderator note (a.p.): branched to a new discussion of software patch to version 1331820?

    Welcome to the community,

    on how to patch a ESXi host from the command line, please see for example http://kb.vmware.com/kb/2008939

    Basically download you the latest hotfix package and install it using the command esxcli . That said, please make sure that your hardware is supported by the patch that you want to apply (see http://www.vmware.com/go/hcl). You can also do before installing the patch, is to do a "dry - run" to know what . VIBS are removed and which are installed by the patch. To do this add -dry - run to the esxcli command line.

    In the case where something goes wrong, you can revert to the previous state by pressing SHIFT-R , while the ESXi hosts begins the start-up.

    André

  • Open a file from the command line

    Hello

    I try to open a pdf file to a specific page from the command line on Windows 10.  I have referenced a few online sources (for example, the stack overflow), but could not find my mistake.  I enter:

    PS to C:\Program Files (x 86) \Adobe\Reader 10.0\Reader >.\AcroRd32.exe 'page = 50' \a "c:\test.pdf".

    When I do, the Player opens with an error message "there was an error opening this document. This file is not found", but clicking on the box OK dialog then opens the file (test.pdf is without a doubt), but the page = 50 command is ignored.

    Can anyone see my mistake?

    Thank you

    Dave

    Memory, /A is not \A.

  • Uninstall creative cloud from the command line

    We have a license for Adobe Creative Cloud 2015.

    I packed Adobe Creative Cloud 2015 Muse for Windows.

    We use Microsoft SCCM to deploy applications packaged in our 8000 machines to Windows 7.

    Using SCCM MS I can install the Muse of 2015 CC packaged from the command line.

    However, how to uninstall an application Adobe Creative Cloud 2015 from the command line?

    Move the discussion to the deployment to the Cloud, business, & CS creative team

  • I have problems with the form widget. When I created my forms, I need to leave out the line, one email because my client does not want the message line and two because those who have tried to fill the online form cannot submit because that box "email".

    I have problems with the form widget. When I created my forms, I need to leave out the line, one email because my client does not want the message line and two because those who have tried to fill the online form cannot submit because the 'email' box keep rejecting their email address valid. And I just tried to remove the line in my form and it does not allow me to delete or to mark it as not necessary either.

    Currently, there is no way around the field email forms of the Muse. Another option is to have a look at Jotforms or another third-party provider of shape that Muse has widgets for.

Maybe you are looking for