我正在使用t-sql来拆除一些XML,然后生成一个新的属性和值,我希望通过xquery / xpath添加回来。请注意,我只是从头开始看了几天......
所以我正在遍历一个包含xml列的表,一次只能处理1行,如果IndAllocID中有一个值,我想把它重新添加到该行的XML中作为全新的。
每当我尝试但我收到以下错误时......有人可以帮忙吗?
XQuery [dbo.OTC_FIXML_Data.FIXML_Data.modify()]:'insert'的目标必须是单个节点,找到'element(CustomUcs,xdt:untyped)*'
这是代码
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 =@Id
从研究错误看来,我正在尝试添加一个可能在文件中不止一次存在的结构?如果是这样,我可以添加到所有事件?
只是添加我希望最终的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"/>
答案 0 :(得分: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]');