如何更改查询以免发生此错误:
XML数据类型方法“ alue”必须是字符串文字
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")。
sql:variable("@VariableName")
因此,您的示例将如下所示:
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