XML 数据类型方法“值"必须是字符串文字 [英] XML data type method “value” must be a string literal

查看:28
本文介绍了XML 数据类型方法“值"必须是字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改我的查询以免发生此错误:

How to change my query so that this error doesn't happen:

XML 数据类型方法value"必须是字符串字面量

XML data type method "value" must be a string literal

T-SQL 代码:

Declare @Count Int = 1 
While(@count <= @j) 
Begin 
insert into mytable 
([Word]) 
Select ([XmlColumn].value(N'word['+Cast(@Count as nvarchar(2))+']/@Entry','nvarchar(max)')) 
    from OtherTable WHERE ID=2

推荐答案

对于 value 方法,您不能以这种方式将变量连接为字符串.您需要使用 sql:variable("@VariableName").

You cannot concatenate variables as strings in this way for the value method. You need to use sql:variable("@VariableName").

所以你的例子应该是这样的:

So your example would be something like this:

Declare @Count Int = 1 
While(@count <= @j) 
Begin 
insert into mytable 
([Word]) 

Select ([XmlColumn].value(N'/word[sql:variable("@Count")]/@Entry)[1]','nvarchar(max)'))
    from OtherTable WHERE ID=2

这篇关于XML 数据类型方法“值"必须是字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆