2014-05-23 30 views
0

我正在使用t-sql來拆除一些XML,然後生成一個新的屬性和值,我希望通過xquery/xpath添加回去。請注意,我只從頭看了幾天......使用新屬性更新sql表中的XML列

所以我遍歷一個表,其中包含一個xml列,每次工作1行,如果有一個值在IndAllocID中,我想將它添加回該行的XML中,作爲全新的。

每當我嘗試雖然我收到以下錯誤....任何人都可以請幫忙嗎?

的XQuery [dbo.OTC_FIXML_Data.FIXML_Data.modify()]: '插入' 的目標必須是單個節點,找到 '元素(CustomUcs,XDT:無類型)*'

這是代碼

declare @indallocid nvarchar(100) 
select @indallocid = (
     select IndAllocID 
     from dbo.OTC_FIXML_Data 
     where MessageRef = @Id 
     )   

update dbo.OTC_FIXML_Data 
set  FIXML_Data.modify('declare namespace ns="http://www.w3.org/2001/XMLSchema";insert attribute CustomEventLink {sql:variable("@indallocid")} 
into ns:FIXML[1]//AllocInstrctn[1]/Instrmt[1]/CustomTag[1]/CustomUcs[1]') 
where MessageRef [email protected] 

從研究錯誤,似乎我試圖添加到一個可能存在不止一次在文件中的結構?如果是的話,我可以添加到所有的事件?

只是增加我怎麼想最終的XML看....新增加的是最後一個屬性

<?xml version="1.0" encoding="UTF-8"?> 
-<FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">- <AllocInstrctn PosEfct="C" NetMny="0000000" BkngTyp="0" SettlDt="2014-04-24" TxnTm="2014-04-23T13:54:36+01:00" TrdDt="2014-04-23" Ccy="GBP" AvgPx="0" PxTyp="9" OrignDt="2014-04-23" LastMkt="UNEX" QtyTyp="0" Qty="34700000"Side="1" NoOrdsTyp="1" LinkTyp="1" LinkID="0000000" ID2="2088767" Typ="1" TransTyp="0" ID="00000000FL"> 
<Hdr TID="" SID=""/> 
<CustomTag CustomOtherCcy="GBP" CustomSettlementMethod="NORMAL" CustomRefRate="000.664516" CustomAuthDt="2014-04-23T12:56:16.68" CustomTrdTm="2014-04-23T13:52:28.41" CustomComments="abc def ghi jkl mnop" CustomTradeStatus="Confirmed" CustomFirstQuotedDt="2014-04-23T13:52:35" CustomFilledDt="2014-04-23T13:54:36.53" CustomWorkingDt="2014-04-23T13:54:36.53" CustomOrdTyp="Market" CustomFillType="N" CustomFillCnt="0" CustomDirectedComm="N" CustomSoftComm="N"/> 
<OrdAlloc ClOrdID2="2088767" ClOrdID="MANUAL"/> 
-<Instrmt ID="ABC0000099XX" IntAcrl="2013-09-24" Desc="GBP UKRPI @ " Issr="Inflation Swap" Exch="UNEX" CpnRt="0" Mult="100" IssuCtry="GB" Fctr="1" CpnPmt="2063-09-24" MatDt="2063-09-24" SubTyp="Inflation Swap" SecTyp="Swap" CFI="" Src="101" Sym="Inflation Swap-FLOAT"> 
-<CustomTag CustomFixingReference="UKRPI" CustomCouponModifier="Following" CustomAccrualConvention="ACT/365" CustomPayFreq="0" CustomAssetSubType="Inflation Swap" CustomAssetType="Swap" CustomAssetSubClass="SWAP" CustomAssetClass="SWAPFL" CustomIssuerCode="HSB" CustomInstrumentStatus="1" CustomRiskCcy="GBP" CustomPriceCcy="GBP" CustomSharesOutstanding="0" CustomSettlDays="3" CustomFactorIndexation="1" CustomFactorAmortisation="1" CustomPrice="0.03713" CustomFixedFloat="2" CustomPaymentType="1" CustomIndexationLag="2" CustomDebtType="<Unassigned>" CustomResetFreq="0" CustomTenor="600" CustomFunded="Y" CustomUnadjFirstCpn="2063-09-24T00:00:00"> 
<CustomUCs Desc="Cash/Physical" Id="604" UC="<Unassigned>" CustomEventLink = "XXXXXX"/> 

回答

2

SQL Server不知道你的into子句中使用表達式識別一個節點只要。

試試這個。

into (FIXML//AllocInstrctn/Instrmt/CustomTag/CustomUcs)[1] 
with xmlnamespaces(default 'http://www.fixprotocol.org/FIXML-4-4') 
update dbo.OTC_FIXML_Data 
set FIXML_Data.modify('insert attribute CustomEventLink {sql:variable("@indallocid")} 
         into (FIXML//AllocInstrctn/Instrmt/CustomTag/CustomUCs)[1]'); 
+0

至少沒有錯誤,但我的XML尚未更新...... – user3008705

+0

@ user3008705如果您發佈XML的樣本,你想什麼它看起來像修改後的我可能能夠幫助你。 –

+0

使用所需的XML編輯原始文章。 – user3008705