在xml中插入属性时,获取"xml数据类型方法的参数1"修改"必须是字符串文字"

sam*_*sam 7 xml t-sql dynamic-sql

尝试以下代码.但是获取'xml数据类型方法的参数1"修改"必须是字符串文字'错误.搜索了很多,但无法找到解决此问题的任何方法

SET @Path = '/@ParentNodeName/@NodeName/child::*'
SET @x.modify('insert attribute status {sql:variable("@status")}
               as first into (' + @Path + ')[1]')
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 11

问题不在于具有您尝试插入的值的sql:变量 - 这是将XPath包含在修改语句中的方式.你不能将该命令串在一起 - 你需要使用文字:

所以你需要使用:

SET @x.modify('insert attribute status {sql:variable("@status")}
               as first into (/Parent/Node/)[1]')
Run Code Online (Sandbox Code Playgroud)

然后它工作得很好.