DB2 If语句在XMLTABLE中不起作用

时间:2016-04-28 13:24:19

标签: xml db2

我有下面的sql,它接受传入的xml,并生成另一个xml。

对于element firstName和lastName,如果属性“encrypted”为true,则我调用一个函数来解密该值。

但不知何故,无论属性“加密”是否为真,sql总是执行调用解密函数的行 - RDS.PCMS_DECRYPTION。

我的sql有问题吗?

由于

with origxml(xdoc) AS (VALUES XMLPARSE( DOCUMENT
    '
    <transactionSale>
        <TxnHeader action="3" class="uk.co.datafit.beanstore.server.TxnHeaderData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <encryptionID>DJ_KEY_2016</encryptionID>
      <externalAccount>R01843390</externalAccount>
      <finishDateTime>2016-04-27 14:20:21.42</finishDateTime>
      <legalTender>AUD</legalTender>
      <loyaltyValue>0.00</loyaltyValue>
      <operatorOperatorCode>52286</operatorOperatorCode>
      <processingTypeValidationCodeConst>58015</processingTypeValidationCodeConst>
      <sellingStore>2116</sellingStore>
      <sourceSystem>GIFTRGSTRY</sourceSystem>
      <startDateTime>2016-04-27 14:19:45.41</startDateTime>
      <status>0</status>
      <storeCode>2116</storeCode>
      <tillNumber>1</tillNumber>
      <tillStatus>0</tillStatus>
      <tradingDate>2012-07-30</tradingDate>
      <transactionNumber>78019</transactionNumber>
      <transactionTypeTransactionType>3</transactionTypeTransactionType>
      <valueDue>183.00</valueDue>
      <valueGross>183.00</valueGross>
      <valueNett>166.36</valueNett>
      <valueRounding>0.00</valueRounding>
      <valueTax>16.64</valueTax>
    </TxnHeader>
    <TransactionQueue action="3" class="uk.co.datafit.beanstore.server.TransactionQueueData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <status>N</status>
      <storeCode>2116</storeCode>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
    </TransactionQueue>
    <TxnDetail action="3" class="uk.co.datafit.beanstore.server.TxnDetailData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <colour>N/A</colour>
      <companyCode>DJ01</companyCode>
      <detailNumber>0</detailNumber>
      <externalStockNumber>4947149</externalStockNumber>
      <extRefNo>1234</extRefNo>
      <fulfilInfo>DELIVER</fulfilInfo>
      <level>0</level>
      <level5CompanyCode>DJ01</level5CompanyCode>
      <level5Level5ID>2743-157</level5Level5ID>
      <level5ProductGroupType>999003</level5ProductGroupType>
      <level5StructureType>1</level5StructureType>
      <operatorOperatorCode>52286</operatorOperatorCode>
      <pluNumber>0027131612278</pluNumber>
      <priceDiscountable>168.00</priceDiscountable>
      <priceSold>168.00</priceSold>
      <quantitySold>1.0</quantitySold>
      <saleDate>2016-04-27 14:20:09.41</saleDate>
      <sellingCompany>DJ01</sellingCompany>
      <sellingDepartmentCode>2743</sellingDepartmentCode>
      <sellingStore>2116</sellingStore>
      <sizeIndicator>50ML</sizeIndicator>
      <skuCode>2743-65773</skuCode>
      <skuOwner>DJ01</skuOwner>
      <skuOwnerType>1</skuOwnerType>
      <storeCode>2116</storeCode>
      <systemPrice>168.00</systemPrice>
      <taxInclusive>1</taxInclusive>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <transLineTypeTransLineType>6</transLineTypeTransLineType>
      <unitOfMeasure>each</unitOfMeasure>
      <valueLine>168.00</valueLine>
      <valueLineAllDiscounts>168.00</valueLineAllDiscounts>
    </TxnDetail>
    <TxnCustomer action="3" class="uk.co.datafit.beanstore.server.TxnCustomerData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <customerStatus>1</customerStatus>
      <customerSubType>0</customerSubType>
      <customerType>1</customerType>
      <firstName encrypted="false">dAUbRNd7BlJNSLLMh7HHDLckpbDXYcm1Lbv9z8r+pVzXGBoQJhuJ9FmraHk534U1zwYk6ENm7P9Z24DzJZHdSEfuxca1v4c/tBQ79XHZWfBQKrYZDqX7WuVvkWm8sxdb4GQ1p4AchZZHR0xWS37xn4hSUgvjas5VuO/jFX3hNCYj+nw67kKqEWzO4Pkr8/iRBc2pGEgCr/ZzhAgafUuTyH1t2pQSn0JIYuulbacaHx3Q9P5WO+r3cb0hpaeHjB1ICKa+c9XLQ1KvqghJLJtTtQQWX1ixbnnbzw+Gnl0GUDitx7cxSF36RlCdIr226TrzGyilWiPi6NW+BH+zlgEp4A==</firstName>
      <gender>79303</gender>
      <lastName encrypted="true">bttvQ/R1pFKl1ILxUeymmFfLRJ6cnWOZQSjNch62S54p2LNczkAJO/xC+kK77TtVff7eh9hI2h4CRm00brhIa9/8yt4Crq+tYaY+VosFp7dCuHL+fYRTrzmWp0DI5tVYtYeIcg5VMbmuvSmpvmDz2zzdmB7GW7Rbh1dooD0oOeBDowPqu3fShCm2HKzFRDLTYnkdRJPne3bzL9BrRWwEeCWyoaca8NHozjsWz3Wfi5hJgwvs+DO4dm5wh4qMr+frAiO2EeszBCvptPzf6z45+MtgOckbgo1izJXwxjBwDN0ddvtnnbuxp+P3ojgVrF2bkj+AH55iE4q77oV181t9WQ==</lastName>
      <sequenceNumber>1</sequenceNumber>
      <storeCode>2116</storeCode>
      <tillNumber>1</tillNumber>
      <title>Mr</title>
      <transactionNumber>78019</transactionNumber>
      <comment>Happy birthday</comment>
    </TxnCustomer>
    <TxnDetailCustomer action="3" class="uk.co.datafit.beanstore.server.TxnDetailCustomerData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <detailNumber>0</detailNumber>
      <quantity>1.0</quantity>
      <sequenceNumber>1</sequenceNumber>
      <storeCode>2116</storeCode>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
    </TxnDetailCustomer>
    <TxnAddress action="3" class="uk.co.datafit.beanstore.server.TxnAddressData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <addressSequenceNumber>1</addressSequenceNumber>
      <addressSubTypeAddressSubType>3</addressSubTypeAddressSubType>
      <companyCode>DJ01</companyCode>
      <customerSequenceNumber>1</customerSequenceNumber>
      <monthsAtAddress>-32768</monthsAtAddress>
      <storeCode>2116</storeCode>
      <telephoneNumber>0452541141</telephoneNumber>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
    </TxnAddress>
    <TxnDetailAddress action="3" class="uk.co.datafit.beanstore.server.TxnDetailAddressData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <detailNumber>0</detailNumber>
      <quantity>1.0</quantity>
      <sequenceNumber>1</sequenceNumber>
      <storeCode>2116</storeCode>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
    </TxnDetailAddress>
    <TxnDetailTaxes action="3" class="uk.co.datafit.beanstore.server.TxnDetailTaxesData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <amount>15.27</amount>
      <companyCode>DJ01</companyCode>
      <detailNumber>0</detailNumber>
      <flatAmountIncTax>0.00</flatAmountIncTax>
      <rate>10.0</rate>
      <storeCode>2116</storeCode>
      <taxAuthorityCode>AU</taxAuthorityCode>
      <taxDate>2016-04-27 14:20:21.69</taxDate>
      <taxNumber>0</taxNumber>
      <taxRuleCode>GSTNONZERO</taxRuleCode>
      <taxRuleName>GST</taxRuleName>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <valueTaxable>168.00</valueTaxable>
    </TxnDetailTaxes>
    <TxnDetail action="3" class="uk.co.datafit.beanstore.server.TxnDetailData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <companyCode>DJ01</companyCode>
      <detailNumber>1</detailNumber>
      <fulfilInfo>DELIVER</fulfilInfo>
      <level>0</level>
      <level5ProductGroupType>0</level5ProductGroupType>
      <level5StructureType>0</level5StructureType>
      <operatorOperatorCode>52286</operatorOperatorCode>
      <priceDiscountable>15.00</priceDiscountable>
      <priceSold>15.00</priceSold>
      <quantitySold>1.0</quantitySold>
      <saleDate>2016-04-27 14:20:14.18</saleDate>
      <sellingCompany>DJ01</sellingCompany>
      <sellingDepartmentCode>601943</sellingDepartmentCode>
      <sellingStore>2116</sellingStore>
      <skuCode>601943-1</skuCode>
      <skuOwner>DJ01</skuOwner>
      <skuOwnerType>1</skuOwnerType>
      <storeCode>2116</storeCode>
      <systemPrice>0.00</systemPrice>
      <taxInclusive>1</taxInclusive>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <transLineTypeTransLineType>22</transLineTypeTransLineType>
      <unitOfMeasure>each</unitOfMeasure>
      <valueLine>15.00</valueLine>
      <valueLineAllDiscounts>15.00</valueLineAllDiscounts>
    </TxnDetail>
    <TxnDetailTaxes action="3" class="uk.co.datafit.beanstore.server.TxnDetailTaxesData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <amount>1.36</amount>
      <companyCode>DJ01</companyCode>
      <detailNumber>1</detailNumber>
      <flatAmountIncTax>0.00</flatAmountIncTax>
      <rate>10.0</rate>
      <storeCode>2116</storeCode>
      <taxAuthorityCode>AU</taxAuthorityCode>
      <taxDate>2016-04-27 14:20:21.69</taxDate>
      <taxNumber>0</taxNumber>
      <taxRuleCode>GSTNONZERO</taxRuleCode>
      <taxRuleName>GST</taxRuleName>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <valueTaxable>15.00</valueTaxable>
    </TxnDetailTaxes>
    <TxnTaxes action="3" class="uk.co.datafit.beanstore.server.TxnTaxesData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <amount>16.64</amount>
      <companyCode>DJ01</companyCode>
      <detailNumber>1</detailNumber>
      <rate>10.0</rate>
      <storeCode>2116</storeCode>
      <taxAuthorityCode>AU</taxAuthorityCode>
      <taxDate>2016-04-27 14:20:21.69</taxDate>
      <taxRuleCode>GSTNONZERO</taxRuleCode>
      <taxRuleName>GST</taxRuleName>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <valueTaxable>183.00</valueTaxable>
    </TxnTaxes>
    <TxnMedia action="3" class="uk.co.datafit.beanstore.server.TxnMediaData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <amountLocal>183.00</amountLocal>
      <companyCode>DJ01</companyCode>
      <detailNumber>0</detailNumber>
      <differenceLocal>0.00</differenceLocal>
      <expectedLocal>183.00</expectedLocal>
      <mediaInfoMediaNumber>1</mediaInfoMediaNumber>
      <mediaInfoMediaTypeMediaType>1</mediaInfoMediaTypeMediaType>
      <reference>170100</reference>
      <state>0</state>
      <storeCode>2116</storeCode>
      <tenderDateTime>2016-04-27 14:20:21.27</tenderDateTime>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <type>10</type>
    </TxnMedia>
    <TxnMedia action="3" class="uk.co.datafit.beanstore.server.TxnMediaData">
      <version>
        <database>3.332.15</database>
        <build>BS45.0.33.0</build>
      </version>
      <amountLocal>0.00</amountLocal>
      <companyCode>DJ01</companyCode>
      <detailNumber>1</detailNumber>
      <differenceLocal>0.00</differenceLocal>
      <expectedLocal>0.00</expectedLocal>
      <mediaInfoMediaNumber>1</mediaInfoMediaNumber>
      <mediaInfoMediaTypeMediaType>1</mediaInfoMediaTypeMediaType>
      <state>0</state>
      <storeCode>2116</storeCode>
      <tenderDateTime>2016-04-27 14:20:21.28</tenderDateTime>
      <tillNumber>1</tillNumber>
      <transactionNumber>78019</transactionNumber>
      <type>11</type>
    </TxnMedia>
  </transactionSale>'
        )
    )
    select xml2clob (txnheader.TxnXml)   from
    (
    select xml_det.TxnXml  
    FROM   origxml,  XMLTABLE('for $a in $c/transactionSale/TxnDetail
                      let $hdrdet := (for $x in $c/transactionSale/TxnHeader
                         return 
                      <hdr> 
                      <storeCode>{if (fn:exists ($x/sellingStore)) then $x/sellingStore/text() else $x/storeCode/text()}</storeCode>
                      <stime>{$x/startDateTime/text()}</stime>
                      </hdr>    )
                      let $strkey := substring(concat("0000",$hdrdet/storeCode),string-length(concat("0000",$hdrdet/storeCode)) -3,4)
                      let $dtekey := fn:replace($hdrdet/stime," ","-")
                      let $trnkey := substring(concat("000000",$a/transactionNumber),string-length(concat("000000",$a/transactionNumber)) -5,6)
                      let $tilkey := substring(concat("000",$a/tillNumber),string-length(concat("000",$a/tillNumber)) -2,3)
                      let $dtlkey := substring(concat("0000",$a/detailNumber),string-length(concat("0000",$a/detailNumber)) -3,4)
                      let $promo := (for $k in $c/transactionSale/InformationLine
                                              where $a/detailNumber = $k/txnDetailNr
                         and $k/valcConst = 430104
                         order by $k/sequenceNr
                       return <x>{xs:decimal($k/additionalInfo1) - xs:decimal($a/priceSold)}</x>)/text()
                      let $product := if(fn:exists($a/externalStockNumber))
                      then $a/externalStockNumber/text()
                      else db2-fn:sqlquery("select xmlcast((PRODUCT_ID) as xml) from rds.RDS_PRODUCT 
                       where DEPARTMENT_NUM = parameter(1) and TRACK_NUM = parameter(2)",
                       if ($a/sellingDepartmentCode <= 9999)  then $a/sellingDepartmentCode else 1,fn:substring-after($a/skuCode,"-")) 
                      let $store   := $a/storeCode
                      let $delivered :=  (for $m in $c/transactionSale/TxnDetail
                        where $m/sellingDepartmentCode = 601943
                        and not(fn:exists($m/itemVoid))
                        return  <y>Y</y>)/text()
                      let $saleamount := $a/valueLineAllDiscounts
                      let $detailno := $a/detailNumber
                      let $deptnum := $a/sellingDepartmentCode
                      let $deptyp :=  db2-fn:sqlquery("select xmlcast((department_typ) as xml) from rds.rds_department
                            where DEPARTMENT_NUM = parameter(1) ",if ($a/sellingDepartmentCode <= 9999)  then $a/sellingDepartmentCode else 1)
                      let $refund  := if ($a/lineRefund = 1) then -1 else 1
                      where not(fn:exists($a/itemVoid))
                      and ($a/transLineTypeTransLineType != 15 
                      and  $a/transLineTypeTransLineType != 21 
                      and  $a/transLineTypeTransLineType != 24 
                      and  $a/transLineTypeTransLineType != 57)
                      return <data>
                        {for $w in $c/transactionSale/TxnHeader
                         where $a/transactionNumber = $w/transactionNumber
                         return <x>
                         <void>{$w/voided/text()}</void>
                         <sourceSystem>{$w/sourceSystem/text()}</sourceSystem>
                         </x>}
                       <J2_GB_SALES>
                       <id>{concat($dtekey,$strkey,$tilkey,$trnkey,$dtlkey)}</id>
                       <processDate>{$trdate}</processDate>
                       <saleDate>{$a/saleDate/text()}</saleDate>
                       <transactionType>{if ($deptnum = 601943) then 3 else if ($refund = -1) then 2 else 1}</transactionType>
                       <storeCode>{$hdrdet/storeCode/text()}</storeCode>
                       {for $h in $c/transactionSale/TxnHeader
                        return 
                        <externalAccount>{$h/externalAccount/text()}</externalAccount>
                       }
                       {for $i in $c/transactionSale/TxnCustomer[1]
                       where $i/customerSubType = 0
                       and   $i/customerType = 1 
                        return 
                        <customer>
                        <customerCode>{$i/customerCode/text()}</customerCode>
                        <title>{$i/title/text()}</title>
                        <firstName> {if ($i/firstName/@encrypted =''true'') then db2-fn:sqlquery(" VALUES xmlcast(TRIM(  substr(RDS.PCMS_DECRYPTION( parameter(1) ),1,20)) as xml) ",$i/firstName/text() ) else $i/firstName/text() } </firstName>                                                            
                        <lastName>  {if ($i/lastName/@encrypted =''true'')  then db2-fn:sqlquery(" VALUES xmlcast(TRIM(  substr(RDS.PCMS_DECRYPTION( parameter(1) ),1,20)) as xml) ",$i/lastName/text() ) else $i/lastName/text() } </lastName>                                                                                    
                        <comment>{$i/comment/text()}</comment>
                        </customer>
                       }
                       {for $i in $c/transactionSale/TxnAddress[1]
                       where $i/addressSubTypeAddressSubType = 3
                       return
                       <telephoneNo>{$i/telephoneNumber/text()}</telephoneNo>
                       }
                       <giftId>{$a/extRefNo/text()}</giftId>
                       <deptNo>{$a/sellingDepartmentCode/text()}</deptNo>
                       <trackNo> {if ($a/sellingDepartmentCode <= 9999 )then fn:substring-after($a/skuCode,"-") else ""}</trackNo>
                       <classNo> {if ($a/sellingDepartmentCode <= 9999 )then fn:substring-after($a/level5Level5ID,"-") else 995}</classNo>
                       <barcode>{if(fn:exists($a/originalBarcode)) then $a/originalBarcode/text() else $a/pluNumber/text()}</barcode>
                       <saleQty>{if ($a/lineRefund = 1) then ($a/quantitySold/text() * -1) else ($a/quantitySold/text()) }</saleQty>

                      {for $e in $c/transactionSale/TxnDetailTaxes
                       where $detailno = $e/detailNumber
                        return 
                        <amounts>
                         <saleAmt>{($saleamount * $refund)}</saleAmt>
                        {for $f in $c/transactionSale/TxnDetailDiscount
                         where $detailno = $f/detailNumber
                         and $f/discountCodeDiscountTypeDiscountType !=8
                         and not(fn:exists($f/voided))
                         and ($f/discountCodeDiscountCode =1 and $f/discountCodeDiscountTypeDiscountType =4)
                         return 
                         <discounts>
                         <discountAmt>{0 - ($f/amount * $refund)}</discountAmt>
                         </discounts>
                         }
                         {for $g in $c/transactionSale/TxnDetailDiscount
                         let $reasoncode   := fn:string($g/reasonReasonCode/text())
                         let $reasontype   := $g/reasonReasonTypeReasonType/text()
                         where $detailno = $g/detailNumber
                         and not(fn:exists($g/voided))
                         and $g/discountCodeDiscountTypeDiscountType !=8
                         and ($g/discountCodeDiscountCode !=1 or $g/discountCodeDiscountTypeDiscountType !=4)
                         return 
                         <reductions>
                         <reductionAmt>{0 - ($g/amount * $refund)}</reductionAmt>
                         </reductions>
                         } 
                         <reductions>
                         <reductionAmt>{if (fn:exists($promo)) then (0 - $promo[1]) else if ($a/priceOverridden = 1 and $a/systemPrice != 0 )then 0 - ($a/systemPrice - $a/priceSold) else 0}</reductionAmt>
                         </reductions>
                        </amounts>
                      }
                       <assistantNo>{$a/operatorOperatorCode/text()}</assistantNo>
                       <productId>{$product}</productId>
                       <registerNo>{$a/tillNumber/text()}</registerNo>
                       <transactionNo>{substring(concat("000000",$a/transactionNumber),string-length(concat("000000",$a/transactionNumber)) -3,4)}</transactionNo>
                       <delivery>{if (fn:exists($delivered)) then "Y" else ""}</delivery>
                       </J2_GB_SALES>
                       </data>'
                            PASSING origxml.xdoc as  "c",
                                    utl.get_process_date_realtime () as "trdate"
                            COLUMNS
                      voided              CHAR(1)         path 'x/void/text()',
                      sourceSystem        varchar(10)     path 'x/sourceSystem/text()',
                      TxnXml              XML            PATH 'J2_GB_SALES/.') as xml_det 
                      where xml_det.voided is null
                      and xml_det.sourceSystem = 'GIFTRGSTRY'
                      )txnheader
with ur

1 个答案:

答案 0 :(得分:0)

作为一个起点。你试过什么样的调试?也许尝试打印 $ i / firstName / @ encrypted $ i / lastName / @ encrypted ,以确保它实际上抓住了您的想法。

还尝试将其更改为false,看看它是否仍在运行,无论如何。

最后根据你发送的内容,它应该在lastName上运行,而不是在firstName上运行。所以我假设问题是它也运行在姓氏上。

我觉得你选择做的很奇怪。 &#39;真正的&#39; &#39;而不是&#39; true&#39;在你的比较中。 我不确定是不是这个原因。我知道在程序上我使用SQL会导致问题。