Solved

I am trying to query xml data stored in a db2 table

Posted on 2015-02-18
12
199 Views
Last Modified: 2015-03-03
I am trying to write a query that will show me the results of xml column in db2 table.   I am not familiar with db2 functions that allows you to query xml columns.  I have include the table definition along with the results in a word document attached.
xml-question.docx
0
Comment
Question by:centralmike
  • 7
  • 5
12 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40618285
>>"The column type is XML.  Is there any way to write a query against just that column that does return all just raw data.  I don’t want to see the xml tags.  "

so you want ALL THE XML DATA
but NO XML TAGS
and only that one column

so this part of the xml:

<?xml version="1.0" encoding="UTF-16" ?><RGLTHOUSANDBYTEASCIIREDEF><FHBDIHTransHdr><DIHTransType>JE_1000Byte_Admin</DIHTransType><DIHTransTypeCdeId>94101</DIHTransTypeCdeId><TransSrc>Admin</TransSrc><MsgTypeId>92001</MsgTypeId><TransStatus/><CtrlGrpID>36069</CtrlGrpID><DetailTransID>738192</DetailTransID><PhysicalGrpID>36068</PhysicalGrpID><DIHSysCdeKey>730091</DIHSysCdeKey><AcctEntryDate>20140603</AcctEntryDate><TransCreditAmt>-00000000000.00


that might look like this (if using commas):

JE_1000Byte_Admin,94101,Admin,92001,,36069,738192,36068,730091,20140603,-00000000000.00


are there any other requirements?
do you want a delimiter instead of the xml tags? if so which delimiter?

finally, as a caution, there may be restrictions on data length that will affect this output
0
 

Author Comment

by:centralmike
ID: 40618746
I think I am trying to get the xml to look a sql result set.   I want the tag elements to be columns and the content to be values.  Hopefully that makes sense.  Maybe I get could load the xml message into its own table.
Exmple
Columms
MsgTypeID     CgtrlGrpID
92001              36069
92003              36071
92004              36074
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40619948
>>" I want the tag elements to be columns"

I understand that statement, but what about the hierarchical nature of XML?
(how do you flatten any hierarchy found in the xml - which can have many one-to-many relationships contained in it)

SQL does not like "dynamic column headings"
 (basically this means you have to list out the tags you actually need (and the rest gets ignored))

and is the XML structure in all rows identical?

I should point out that DB2 isn't something I have used a lot and its XML handling I'm not familiar with, so although I'm asking for clarifications that would assist in reaching a solution, I may not be able to produce that query. I  don't have access to a DB2 instance to test anything with either.
0
 

Author Comment

by:centralmike
ID: 40620445
Paul I am attaching a new file that should give you a better understanding of what I am trying to accomplish.
Paul.docx
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40620454
Ok, this look much more manageable.

q1:.
One that returns the following elements:
<CtrlGrpID>36069</CtrlGrpID>
<TransCreditAmt>-00000000000.00</TransCreditAmt>
<TransDebitAmt>000000000025.00</TransDebitAmt>
The ctrlgrpid makes the row unique.
q2:
The second query should return the following elements:
<CtrlGrpID>36069</CtrlGrpID>
<DEBIT_CREDIT_IND> d </DEBIT_CREDIT_IND>
<AcctEntryStatsAmt> 000000000000.00</AcctEntryStatsAmt>
<AcctEntryAmt> 000000000000.00</AcctEntryAmt>

The XML inside that word doc isn't complete (hence not well formed), do you have it available in a true XML file I can look at? (this helps arrive at the xpaths)
0
 

Author Comment

by:centralmike
ID: 40621160
Good morning Paul, I just copied the xml out of the results that came back from the db2 query.  I think there a couple of different db2 tables in the xml statement.  This just one record in the file.  Each record produces this xml statement with different values.  Here is another copy of the xml:

<?xml version="1.0" encoding="UTF-16" ?><AddFinclTrans><FHBDIHTransHdr><DIHTransType>JE_1000Byte_Admin</DIHTransType><DIHTransTypeCdeId>94101</DIHTransTypeCdeId><MsgTypeId>92001</MsgTypeId><TransSrc>Admin</TransSrc><CtrlGrpID>37323</CtrlGrpID><DetailTransID>3173846</DetailTransID><PhysicalGrpID>37322</PhysicalGrpID><DIHSysCdeKey>730041</DIHSysCdeKey><ChartfieldVersion>9.2</ChartfieldVersion><TransCreditAmt> 000000000000.00</TransCreditAmt><TransDebitAmt>000000000000.00</TransDebitAmt><SequenceNum>00008782</SequenceNum><RecordCreateDate>20150113</RecordCreateDate><RunTspDate>20150113124817</RunTspDate><AcctEntryDate>20150122</AcctEntryDate><DIHRunTspDate>2015-01-13 14:24:32.000092</DIHRunTspDate></FHBDIHTransHdr><JrnlEntryTrans><FinclTrans><SysCde>GFS-</SysCde><DIHSysCdeKey>730041</DIHSysCdeKey><CurrencyCde>USD</CurrencyCde><RecordCreateDate>20150113</RecordCreateDate><RunTspDate>20150113124817</RunTspDate><SequenceNum>00008782</SequenceNum><AcctEntry><AcctNum>202020</AcctNum><AcctEntryAmt> 000000000000.00</AcctEntryAmt><RateTypeCde/><DebitCreditInd>C</DebitCreditInd><CurrencyCde>USD</CurrencyCde><AcctEntryDate>20150122</AcctEntryDate><JrnlSrcCde>GFS</JrnlSrcCde><ProductCde>104110</ProductCde><JrnlLnDesc>GFS - GROUP FINANCIAL SYSTEM</JrnlLnDesc><PrjCde/><DeptCde/><FcnCde/><OprUnitCde>1900</OprUnitCde><InsType>D</InsType><GLBU>UL010</GLBU><BU>UL010</BU><AfflBU/><BookCde>CASH</BookCde><LedgerGrpCde>ACTUALS</LedgerGrpCde><AcctEntryResiStateCde>MI</AcctEntryResiStateCde><AcctEntryStatsAmt> 000000000000.00</AcctEntryStatsAmt><AcctEntryFrgnAmt> 000000000000.00</AcctEntryFrgnAmt><FrgnCurrencyCde>USD</FrgnCurrencyCde><JrnlLnRef/><ExchRate>0000000.00000000</ExchRate><AcctEntryProperty><BusUnitXRefRuleCde/><BusUnitXRefCmpnyCde/><BusUnitXRefSiteCde/><BusUnitXRefRegionCde/><BusUnitXRefPolSeriesCde/><AffilXRefRuleCde/><AffilXRefCmpnyCde/><AffilXRefSiteCde/><AffilXRefRegionCde/><AffilXRefPolSeriesCde/><AcctXRefRuleCde/><AcctXRefAcctCde/><AcctXRefCmpnyCde/><AcctXRefSrcBenefitCde/><ProdXRefRuleCde>GFS</ProdXRefRuleCde><ProdXRefIdent>76H0A9-583446</ProdXRefIdent><ProdXRefPlanCde/><ProdXRefSrcFundMethodCde>1</ProdXRefSrcFundMethodCde><ProdXRefSrcCoverageRiskCde>02</ProdXRefSrcCoverageRiskCde><ProdXRefCmpnyCde/><ProdXRefNonQualifiedInd/><ProdXRefDiscretionaryGrpCde/><ProdXRefSrcStatSeriesNum/><ProdXRefPolSeriesCde/><ProdXRefAcctCde/><ProdXRefSrcStatLOBCde/><SrcXRefRuleCde/><SrcXRefCmpnyCde/><SrcXRefSrcCde/><SrcXRefDeptNum/><SrcXRefSubSysCde/><PolStatXRefRuleCde/><PolStatXRefPolStatusCde/><DeptXRefRuleCde/><DeptXRefAdminCmpyCde/><DeptXRefPolRegionCde/><DeptXRefUnitedTranslationPolNum/><DeptXRefUnitedPensionTrustCde/><DeptXRefBillTypeCde/><DiscretionaryGrpCde/><SrcXrefSrcSubSysCde>GX$</SrcXrefSrcSubSysCde><FODSProdXRefSrcPolSeriesNum>23</FODSProdXRefSrcPolSeriesNum><FODSProdXRefSrcStatSeriesNum/><FODSProdXRefSrcStatLOBCde/><AgentNum/><AllocationIndexCde/><BudgetCenterNum/><CategoryNum/><CountyCde/><GFSReconNum>68L8RH-L3H295000008782</GFSReconNum><GLRecordInd/><ManufacturingCmpnyCde/><ManagerNum/><OfficeTownNum/><OriginalAcctNum>061000</OriginalAcctNum><PymtDate/><MediaTypeCde>E</MediaTypeCde><PymtRefNum>E9916056</PymtRefNum><PolClaimIncurYear>2014</PolClaimIncurYear><PolNum>GUG GUG GUG 0ABNG</PolNum><PremiumDivisionIdent>001A</PremiumDivisionIdent><RecordIdent/><RefFieldOneIdent/><RefFieldOneValueNum/><RefFieldTwoIdent/><RefFieldTwoValueNum/><RefFieldThreeIdent/><RefFieldThreeValueNum/><RenewabilityCategoryCde/><TransCde/><TownCde/><UnitedPensionTrustCde/><VchrNum/><ExpenseIncurDate/><InvcNum/><ApplicationTransType/><AnnuityFundCde/><CapsilBranchCde/><SrcTypeFormCde/><TranscendCashBatchNum/><UserAreaDescText/><UserAreaNameText/><PSOriginAP/><InitialSysCde/><InitialDateTsp/><CurrentSysCde/><UnitedPolCoverageFaceAmt>000000000.00</UnitedPolCoverageFaceAmt><Fas97ProductInd/><BaseRiderCde/><PlanName/><StateApplicationSignCde/><TransEffectiveDate/><CorporatePolStatusCde/><BusinessEventActionCde/><FundValueErrorCde/><PolStatusCde/><VantageTransCde/><VantageMemoCde/><TranscendTransCde/><GrpFinclSysTransTypeCde>00204</GrpFinclSysTransTypeCde><InitialPremiumActivityCde/><GrpingNum/><CompensationProductGrpCde/><CompensationRuleNum/><ProcessActivityCde/><ClaimNum>142930004501</ClaimNum><BillAcctNum/><PlanCde/><ParticipatingInd>N</ParticipatingInd></AcctEntryProperty></AcctEntry><AgreementGrp><Pol><PolNum>GUG GUG GUG 0ABNG</PolNum><AgreementID>-1</AgreementID></Pol><BlockInd><BlockIndCde/></BlockInd><Base><PolNum>GUG GUG GUG 0ABNG</PolNum></Base></AgreementGrp><ContactGrp><PayeeContact><ContactPointID>131275347</ContactPointID></PayeeContact></ContactGrp><TransAmt> 000000000000.00</TransAmt></FinclTrans></JrnlEntryTrans></AddFinclTrans>

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 40622754
xpaths for query1 involve:

/AddFinclTrans/FHBDIHTransHdr/CtrlGrpID[text()="37323"]
/AddFinclTrans/FHBDIHTransHdr/TransCreditAmt[normalize-space() ="000000000000.00"]
/AddFinclTrans/FHBDIHTransHdr/TransDebitAmt[text()="000000000000.00"]

xpaths for query2 involve:

/AddFinclTrans/FHBDIHTransHdr/CtrlGrpID[text()="37323"]
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/DebitCreditInd[text()="C"]
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/AcctEntryStatsAmt[normalize-space() ="000000000000.00"]
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/AcctEntryAmt[normalize-space() ="000000000000.00"]
0
 

Author Comment

by:centralmike
ID: 40628180
Paul, I received some sample code :  How can I incorporate your logic in this sql statement?  How can I run your logic in a sql statement?


Sample Code

select  
           message_group_id
          ,sum(y.acct_entry_amt) as amount
          ,y.debit_credit_ind
          ,sum(y.acct_entry_stat_amt) as stat_amount
          ,count(*) as count
          ,error_entry_status_cde
          ,label
             from UFHB000.T_ERROR_ENTRY_LINE
             ,XMLTABLE('$X/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry'
                 passing entry_line_xml as X
            columns
            acct_entry_amt Decimal (23,3) path 'AcctEntryAmt'  
            ,debit_credit_ind char(6) path 'DebitCreditInd'
            ,acct_entry_stat_amt Decimal (23,3) path 'AcctEntryStatsAmt'           ) as y
           ,ufhb000.t_code
       

where create_tsp > '2015-02-06 08:08:01.349031'
--where valid_from_tsp > '2015-02-06 08:08:01.349031'
and err_entry_line_type_cde = 'JE'
and error_entry_status_cde = code_id
group by
                 message_group_id
                ,debit_credit_ind
                ,error_entry_status_cde
                ,label
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40629098
I should say up front please just ignore "[normalize-space() ="000000000000.00"]" in the xpaths I supplied earlier
----------

That (very useful) example looks like your query 2 almost and it indicates you specify a path

$X/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry

and then once at that location, list the nodes you want that belong to that path

            acct_entry_amt Decimal (23,3) path 'AcctEntryAmt'  
            ,debit_credit_ind char(6) path 'DebitCreditInd'
            ,acct_entry_stat_amt Decimal (23,3) path 'AcctEntryStatsAmt'           ) as y

(well that's what it looks like to me)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40638833
Hi, not sure where you got to with this. Solved I hope.

I have had a go at applying the sample syntax to your paths, and my attempt is below. Regret I cannot test this as I haven't access to a DB2  server.

xpaths for query1 involve:

/AddFinclTrans/FHBDIHTransHdr/CtrlGrpID
/AddFinclTrans/FHBDIHTransHdr/TransCreditAmt
/AddFinclTrans/FHBDIHTransHdr/TransDebitAmt

SELECT
      y.CtrlGrpID
	, y.TransCreditAmt
	, y.TransDebitAmt
      /* more ... */
FROM UFHB000.T_ERROR_ENTRY_LINE
	, XMLTABLE(
        '$X/AddFinclTrans/FHBDIHTransHdr' passing entry_line_xml AS X 
            columns 
                path 'CtrlGrpID', CtrlGrpID CHAR(6) 
                path 'TransCreditAmt', TransCreditAmt DECIMAL(23, 3) 
                path 'TransDebitAmt', TransDebitAmt DECIMAL(23, 3) 
        ) AS y
;

Open in new window


xpaths for query2 involve:

/AddFinclTrans/FHBDIHTransHdr/CtrlGrpID
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/DebitCreditInd
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/AcctEntryStatsAmt
/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry/AcctEntryAmt  

SELECT
      y.CtrlGrpID
    , y1.debit_credit_ind
	, y1.TransCreditAmt
	, y1.TransDebitAmt
      /* more ... */
FROM UFHB000.T_ERROR_ENTRY_LINE
	, XMLTABLE(
        '$X/AddFinclTrans/FHBDIHTransHdr' passing entry_line_xml AS X 
            columns 
                path 'CtrlGrpID', CtrlGrpID CHAR(6) 
                path 'TransCreditAmt', TransCreditAmt DECIMAL(23, 3) 
                path 'TransDebitAmt', TransDebitAmt DECIMAL(23, 3) 
        ) AS y
	, XMLTABLE(
        '$X/AddFinclTrans/JrnlEntryTrans/FinclTrans/AcctEntry' passing entry_line_xml AS X1 
            columns 
                path 'DebitCreditInd', DebitCreditInd CHAR(6) 
                path 'AcctEntryStatsAmt', AcctEntryStatsAmt DECIMAL(23, 3) 
                path 'AcctEntryAmt', AcctEntryAmt DECIMAL(23, 3) 
        ) AS y1
;

Open in new window

0
 

Author Comment

by:centralmike
ID: 40641831
Thanks Paul, That really helps.  Might you know of a good manual that I could purchase that would help me learn about XML ,XML TABLES AND XML PATHS
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40641871
no i haven't any references for db2 and xml Sorry

There was a "DB2 cookbook" that was free but the verizon site has disappeared
i dont recall if that govered xml though

You could ask that as a seperate question
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now