【问题标题】:XQuery insert/update valueXQuery 插入/更新值
【发布时间】:2014-11-08 10:39:20
【问题描述】:

我想像这样更新xml

<?xml version="1.0" encoding="utf-16"?>
<Properties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <BackupPath>MyLocalPath</BackupPath>
</Properties>

使用存储过程

declare @PropertyName       nvarchar(500)
declare @PropertyValue      nvarchar(MAX)

set @PropertyName = 'BackupPath'
set @PropertyValue = 'MyTestPath'

DECLARE @ExistingSettings XML;
DECLARE @NewSettings nvarchar(MAX);

SET @ExistingSettings = (SELECT TOP(1) CAST(SettingsDefinition  AS XML) FROM ApplicationSettings)

SET @ExistingSettings.modify('replace value of (/Properties/*[local-name() = sql:variable("@PropertyName")]/text())[1] with sql:variable("@PropertyValue")')

UPDATE ApplicationSettings
SET SettingsDefinition = CAST(@ExistingSettings AS nvarchar(MAX)), LastUpdate = GetDate()
WHERE ID = (SELECT TOP(1) ID FROM ApplicationSettings)

只要“BackupPath”包含值,它就可以正常工作

<BackupPath>MyLocalPath</BackupPath>

如果

<BackupPath />

【问题讨论】:

    标签: sql xquery


    【解决方案1】:

    只是不需要 XPath 中的文本节点。

    SET @ExistingSettings.modify(
      'replace value of (/Properties/' + @PropertyName + ')[1] with sql:variable("@PropertyValue")'
    )
    

    【讨论】:

    • 我现在收到一条错误消息。 XML 数据类型方法“modify”的参数 1 必须是字符串字面量。
    • 无论如何,这种方法有效吗? stackoverflow.com/a/21457475/18771
    猜你喜欢
    • 2023-03-15
    • 2015-08-31
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多