Question

I'm not sure if I can do this in PL/SQL, or if instead I need to just build my XML tree in my web service from multiple output cursors. But I read a little about hierarchical queries in oracle and it seemed like a more elegant solution. I'm very inexperienced with SQL though, so I'm having a difficult time applying examples of hierarchical queries to my case.

I have 4 tables in this hierarchy.

  1. SHIPMENTS :: KEYS {INTERNAL_ASN, BILL_OF_LADING}
  2. ORDERS :: KEYS {INTERNAL_ASN, BILL_OF_LADING, PO_NO}
  3. CARTONS :: KEYS {INTERNAL_ASN, BILL_OF_LADING, PO_NO, CARTON_NO}
  4. ITEMS :: KEYS {INTERNAL_ASN, BILL_OF_LADING, PO_NO, CARTON_NO, UPC_NO}

I want the dataset that's returned to eventually end up as XML simplified as something like this:

  <DATA>
  <SHIPMENTS>
    <SHIPMENT>
      <ORDERS>
        <ORDER>
          <CARTONS>
            <CARTON>
              <ITEMS>
                <ITEM></ITEM>
                <ITEM></ITEM>
                <ITEM></ITEM>
              </ITEMS>
            </CARTON>
            <CARTON>
              <ITEMS>
                <ITEM></ITEM>
                <ITEM></ITEM>
              </ITEMS>
            </CARTON>
          </CARTONS>
        </ORDER>
        <ORDER></ORDER>
      </ORDERS>
    </SHIPMENT> 
  </SHIPMENTS>
</DATA>

What makes my case a little more difficult, is my selection of things I bring back, actually starts at the carton level. A query like this gives me all the carton rows I need to backwards and forwards from.

SELECT *
FROM  
Q194977.AN_CARTON_INFO CI
WHERE
CI.PO_NO = 4887960
AND CI.STORE_NO = 1560

Here is the documentation I found on Oracle Hierarchical queries, but their examples are all with the same table.

http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm#i2060615

Any expert opinions on how I should of approach this?

Was it helpful?

Solution

The Oracle hierachical or connect-by query is different from what you need. You can use a single query to generate the structure you need, but you need to use Oracle XML SQL functions. Here is a link to the Oracle document:

http://docs.oracle.com/cd/E11882_01/appdev.112/e16659/xdb13gen.htm#i1029583

and here is something you may end up with:

    SELECT
       XMLElement("CARTONS", 
          XMLAgg (
             XMLElement("CARTON", 
                XMLForest(carton_id As "CARTON_ID", carton_name As "NAME"),
                 ( 
                   select XMLElement("ITEMS",
                            XMLAgg( 
                               XMLElement("ITEM", 
                                  XMLForest(item_id As "ITEM_ID", description As "DESCRIPTION")
                               )
                            )
                         )             
                     from ITEMS
                   where carton_id = c.carton_id
                )
             )
          )
       ) As myxml
     FROM CARTONS c

for producing:

          <CARTONS>
            <CARTON>
              <ITEMS>
                <ITEM></ITEM>
                <ITEM></ITEM>
                <ITEM></ITEM>
              </ITEMS>
            </CARTON>
            <CARTON>
              <ITEMS>
                <ITEM></ITEM>
                <ITEM></ITEM>
              </ITEMS>
            </CARTON>
          </CARTONS>

The above query starts from your carton level. It will get more complex when you add shipment and order layers. You basically construct the structure in the query, but it is definitely doable.

I wish you were producing JSON from a REST API. Then you could try a PL/SQL framework I have done. This framework provides a utility to produce complex JSON structure from regular queries. In case that you or any other people are curious to know, here is a link to our site:

http://backlogic.net

You may find some interesting examples in Section 5.3 of the [User Guide][1]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top