如何通过更新插入新的xml元素并用SQL Server Mamangement Studio替换

问题描述 投票:0回答:1

我有这个XML:

<qrcode xmlns="http://vital.nl/qrcodeSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Echeq>
    <echeqFamilyId>67c8590e-66fe-43e1-9124-a2163a6365df</echeqFamilyId>
  </Echeq>
</qrcode>

但是我想得到这个:

<qrcode xmlns="http://vital.nl/qrcodeSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Echeq>
 <awardedVPoints>0</awardedVPoints>
    <echeqFamilyId>67c8590e-66fe-43e1-9124-a2163a6365df</echeqFamilyId>
  </Echeq>
</qrcode>

我试图得到这样的结果:

UPDATE[dbo].[QRCodeDefinitions]     
SET[QRCodeXml] = CAST(REPLACE(CAST([QRCodeXml] as nvarchar(max)), '',  '<awardedVPoints>0') AS xml) 
FROM [dbo].[QRCodeDefinitions] 
WHERE qrcode = 'newvcheq'

但是xml中没有任何变化。

所以我必须改变什么?

谢谢

好,所以我尝试了这个:

;WITH XMLNAMESPACES(N'http://vital10.nl/qrcodeSchema.xsd' AS ns)
UPDATE [dbo].[QRCodeDefinitions]
SET QRCodeXml = QRCodeXml.query('
    declare default element namespace "http://vital.nl/qrcodeSchema.xsd";
    <qrcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    {
        for $x in ns:/qrcode/*
        return (
          if (empty($x/../awardedVPoints)) then (<awardedVPoints> {"0"}</awardedVPoints>)
          else (), $x)
    }
    </qrcode>')
FROM [dbo].[QRCodeDefinitions]
WHERE qrcode = 'newvcheq'

但是后来我明白了:

Msg 9332,第16级,状态1,第38行 XQuery [dbo.QRCodeDefinitions.QRCodeXml.query()]:':'附近的语法错误,预期为'where','(stable)order by'或'return'。

如果我这样做:

UPDATE [dbo].[QRCodeDefinitions]
SET [QRCodeXml] = [QRCodeXml].query('
declare default element namespace "http://vital.nl/qrcodeSchema.xsd";
<qrcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
{
    for $x in /qrcode/*
    return (
        if (empty($x/../awardedVPoints)) then (<awardedVPoints> {"0"}</awardedVPoints>)
        else (), $x)
}
</qrcode>')
FROM [dbo].[QRCodeDefinitions]
WHERE qrcode = 'newvcheq'

然后我收到此错误:

XML验证:无效的内容。期望的元素:'{http://vital.nl/qrcodeSchema.xsd} ExternalUrl','{http://vital.nl/qrcodeSchema.xsd}平台','{http://vital.nl/qrcodeSchema.xsd}图像','{http://vital10.nl/qrcodeSchema.xsd}位置','{http://vital.nl/qrcodeSchema.xsd} Echeq' ,“ {http://vital.nl/qrcodeSchema.xsd}视频”。找到:代替元素'{http://vital10.nl/qrcodeSchema.xsd} awardedVPoints'。位置:/ :qrcode [1] /:awardedVPoints [1]。

所以我必须改变什么?谢谢

表设计。这是表格的详细信息。与所有表格字段

CREATE TABLE [dbo].[QRCodeDefinitions]
(
    [QRCode] [nvarchar](256) NOT NULL,
    [Title] [nvarchar](256) NOT NULL,
    [Description] [nvarchar](max) NOT NULL,
    [CreatedBy] [uniqueidentifier] NOT NULL,
    [CreatedOn] [datetime2](7) NOT NULL,
    [UpdatedOn] [datetime2](7) NOT NULL,
    [ValidUntil] [datetime2](7) NOT NULL,
    [LastScannedOn] [datetime2](7) NULL,
    [TotalScanned] [bigint] NOT NULL,
    [MaxAllowedToScan] [bigint] NULL,
    [MaxAllowedToScanByParticipant] [bigint] NULL,
    [ScanFrequency] [bigint] NULL,
    [ScanFrequencyType] [int] NOT NULL,
    [ActionType] [int] NOT NULL,
    [QRCodeXml] [xml](CONTENT [dbo].[QRCodeSchema-2019-07-23]) NULL,
    [PublishState] [int] NOT NULL,
 CONSTRAINT [PK_QRCodeDefinitions] PRIMARY KEY CLUSTERED 
(
    [QRCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[QRCodeDefinitions] ADD  DEFAULT ((0)) FOR [PublishState]
GO

所以这必须是正确的答案:

UPDATE [dbo].[QRCodeDefinitions]
SET QRCodeXml = QRCodeXml.query('
declare default element namespace "http://vital10.nl/qrcodeSchema.xsd";
<qrcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
{
    for $x in /qrcode/*
    return (
        if (empty($x/../awardedVPoints)) then (<awardedVPoints>{"0"}</awardedVPoints>)
        else (), $x)
}
</qrcode>')
FROM[dbo].[QRCodeDefinitions]
WHERE qrcode = 'newvcheq';
sql-server xml tsql xsd xquery
1个回答
0
投票

请尝试以下解决方案。它正在使用XQuery及其FLWOR表达式。

© www.soinside.com 2019 - 2024. All rights reserved.