[英]XQuery insert/update value
I want to update xml like this one 我想像这样更新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>
using stored procedure 使用存储过程
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)
and this works fine as long as 'BackupPath' contains value 只要“ BackupPath”包含值,此方法就可以正常工作
<BackupPath>MyLocalPath</BackupPath>
How to modify this query to insert value (or validate if needed) if 如何修改此查询以插入值(或根据需要验证),如果
<BackupPath />
Just don't require a text node in your XPath. 只是不需要在XPath中使用文本节点。
SET @ExistingSettings.modify(
'replace value of (/Properties/' + @PropertyName + ')[1] with sql:variable("@PropertyValue")'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.