Parameterizing XPath for modify() in SQL Server XML Processing(在 SQL Server XML 处理中为 modify() 参数化 XPath)
问题描述
正如标题所暗示的那样,我正在尝试为 SQL Server 中 XML 数据列的 modify() 方法参数化 XPath,但遇到了一些问题.
Just like the title suggests, I'm trying to parameterize the XPath for a modify() method for an XML data column in SQL Server, but running into some problems.
到目前为止我有:
DECLARE @newVal varchar(50)
DECLARE @xmlQuery varchar(50)
SELECT @newVal = 'features'
SELECT @xmlQuery = 'settings/resources/type/text()'
UPDATE [dbo].[Users]
SET [SettingsXml].modify('
replace value of (sql:variable("@xmlQuery"))[1]
with sql:variable("@newVal")')
WHERE UserId = 1
具有以下 XML 结构:
with the following XML Structure:
<settings>
...
<resources>
<type> ... </type>
...
</resources>
...
</settings>
然后产生这个错误:
XQuery [dbo.Users.NewSettingsXml.modify()]:'replace'的目标最多只能是一个节点,找到'xs:string ?'
现在我意识到修改方法一定不能接受字符串作为路径,但是有没有办法不使用动态 SQL 来实现这一点?
Now I realize that the modify method must not be capable of accepting a string as a path, but is there a way to accomplish this short of using dynamic SQL?
哦,顺便说一下,我使用的是 64 位 SQL Server 2008 Standard,但我编写的任何查询都需要与 2005 Standard 兼容.
Oh, by the way, I'm using SQL Server 2008 Standard 64-bit, but any queries I write need to be compatible back to 2005 Standard.
谢谢!
推荐答案
如果有人感兴趣,我自己使用动态查询想出了一个相当不错的解决方案:
In case anyone was interested, I came up with a pretty decent solution myself using a dynamic query:
DECLARE @newVal nvarchar(max)
DECLARE @xmlQuery nvarchar(max)
DECLARE @id int
SET @newVal = 'foo'
SET @xmlQuery = '/root/node/leaf/text()'
SET @id = 1
DECLARE @query nvarchar(max)
SET @query = '
UPDATE [Table]
SET [XmlColumn].modify(''
replace value of (' + @xmlQuery + '))[1]
with sql:variable("@newVal")'')
WHERE Id = @id'
EXEC sp_executesql @query,
N'@newVal nvarchar(max) @id int',
@newVal, @id
使用它,动态查询中唯一不安全的部分是 xPath,就我而言,它完全由我的代码控制,因此不应被利用.
Using this, the only unsafe part of the dynamic query is the xPath, which, in my case, is controlled entirely by my code and so shouldn't be exploitable.
这篇关于在 SQL Server XML 处理中为 modify() 参数化 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 SQL Server XML 处理中为 modify() 参数化 XPath


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01