【问题标题】:Updating XML column in a sql table with new attribute使用新属性更新 sql 表中的 XML 列
【发布时间】:2014-05-23 11:00:24
【问题描述】:

我正在使用 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"/>

【问题讨论】:

    标签: xml tsql xpath xquery


    【解决方案1】:

    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]');
    

    【讨论】:

    • @user3008705 如果您发布 XML 示例以及您希望修改后的样子,我可能会为您提供帮助。
    • 使用所需的 XML 编辑原始帖子。
    • @user3008705 您在问题中的 XML 无效。但我猜到了你想要什么,你使用了错误的命名空间。
    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2011-07-17
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2018-12-04
    相关资源
    最近更新 更多