Sql XML数据类型方法的参数1“;价值”;必须是字符串文字
如果我传递@count变量,我就会得到这个错误 下面是我的问题Sql XML数据类型方法的参数1“;价值”;必须是字符串文字,sql,sql-server,xml,tsql,Sql,Sql Server,Xml,Tsql,如果我传递@count变量,我就会得到这个错误 下面是我的问题 DECLARE @Error_Description NVARCHAR(Max) DECLARE @Count VARCHAR(20) DECLARE @x NVARCHAR(Max) SELECT @Error_Description = 'The external columns for Excel Source are out of synchronization with the data source columns.
DECLARE @Error_Description NVARCHAR(Max)
DECLARE @Count VARCHAR(20)
DECLARE @x NVARCHAR(Max)
SELECT @Error_Description = 'The external columns for Excel Source are out of synchronization with the data source columns.
The column "szReferencceNumber" needs to be added to the external columns.
The column "SMSa" needs to be added to the external columns.
The column "as" needs to be added to the external columns.'
SELECT @Count = (LEN(@Error_Description) - LEN(REPLACE(@Error_Description, '"', ''))) / LEN('"')
SELECT @Count
SELECT COALESCE(LTRIM(CAST(('<X>' + REPLACE(@Error_Description, '"', '</X><X>') + '</X>') AS XML).value('(/X)[' + @Count + ']', 'varchar(128)')), '')
DECLARE@Error\u Description NVARCHAR(最大值)
声明@Count VARCHAR(20)
声明@x NVARCHAR(最大值)
选择@Error_Description=”Excel源的外部列与数据源列不同步。
“szReferenceNumber”列需要添加到外部列。
需要将“SMSa”列添加到外部列。
“as”列需要添加到外部列中。”
选择@Count=(LEN(@Error_Description)-LEN(REPLACE(@Error_Description,“,”)/LEN(“”)
选择@Count
选择合并(LTRIM(强制转换(“+REPLACE(@Error_Description,”,“)+”)为XML)。值(“(/X)['+@Count+'],'varchar(128)”),”)
值的第一个参数必须是字符串文本。要选择具有动态索引的节点,可以执行以下操作
SELECT
n.value('.', 'varchar(128)') as Result
from (SELECT CAST(('<X>' + REPLACE(@Error_Description, '"', '</X><X>') + '</X>') AS XML)) ca(x)
CROSS APPLY x.nodes('(/X)') n(n)
WHERE n.value('for $l in . return count(../*[. << $l]) + 1', 'int') %2 = 0
value
的第一个参数必须是字符串文本
SELECT
n.value('.', 'varchar(128)') as Result
from (SELECT CAST(('<X>' + REPLACE(@Error_Description, '"', '</X><X>') + '</X>') AS XML)) ca(x)
CROSS APPLY x.nodes('(/X)') n(n)
WHERE n.value('for $l in . return count(../*[. << $l]) + 1', 'int') %2 = 0
如果您使用的是2012+,并且可以使用nvarchar(4000)
(而不是MAX)
,则可以获取ItemNumber
值为偶数的行的副本并抓取这些行:
DECLARE @Error_Description nvarchar(4000);
SELECT @Error_Description = N'The external columns for Excel Source are out of synchronization with the data source columns.
The column "szReferencceNumber" needs to be added to the external columns.
The column "SMSa" needs to be added to the external columns.
The column "as" needs to be added to the external columns.';
SELECT DS.Item
FROM dbo.DelimitedSplitN4K_LEAD(@Error_Description,'"') DS
WHERE DS.ItemNumber % 2 = 0;
如果您使用的是SQL server 2016+,则可以使用一些JSON操作(支持MAX
值):
如果您使用的是2012+,并且可以使用nvarchar(4000)
(而不是MAX)
,则可以获取ItemNumber
值为偶数的行的副本并抓取这些行:
DECLARE @Error_Description nvarchar(4000);
SELECT @Error_Description = N'The external columns for Excel Source are out of synchronization with the data source columns.
The column "szReferencceNumber" needs to be added to the external columns.
The column "SMSa" needs to be added to the external columns.
The column "as" needs to be added to the external columns.';
SELECT DS.Item
FROM dbo.DelimitedSplitN4K_LEAD(@Error_Description,'"') DS
WHERE DS.ItemNumber % 2 = 0;
如果您使用的是SQL server 2016+,则可以使用一些JSON操作(支持MAX
值):
您可以在XQuery谓词中使用您的@Count
,但不能通过连接。有sql:variable()
:
将变量@Count
声明为INT
将有助于避免XQuery强制转换
提示:您需要最后的[1]
来强制执行的单例.value()
要求。您可以在XQuery谓词中使用@Count
,但不能通过串联。有sql:variable()
:
将变量@Count
声明为INT
将有助于避免XQuery强制转换
提示:您需要最后一个[1]
来强制执行单例.value()
的要求。这里的错误非常清楚。您的实际目标是什么?@Larnu我需要引号之间的字符串。如果我运行此查询,它工作正常。选择COALESCE(LTRIM(CAST('+REPLACE(@error\u Description,','')+'')作为XML)。value('/X)[2]“,”varchar(128)”,”)
我将问题复制为@Larnu您是否更喜欢将数据存储在表变量中。然后我们可以使用cross-apply,因为错误消息说第一个参数需要是字符串文本。不是像'(/X)['+@Count+'].
]这样的表达式-因此,您可以通过动态构造整个SQL字符串并将@Count
连接到中来实现-但是如果您解释您尝试执行的操作的实际最终要求,可能有一种更好的方法,在引号中有多个字符串(“
)虽然@SubhashManikantaKumarMogili。你没有解释你的真正目标是什么,所以我们很难帮助你。这里的错误很清楚。你的实际目标是什么?@Larnu我需要引号之间的字符串。如果我运行这个查询,它写得很好。选择COALESCE(LTRIM(CAST)(“+REPLACE”(@error_Description,”,“+”)作为XML).value(“(/X)[2]”、“varchar(128)”),“”)
我将此问题复制为@Larnu您是否希望将数据存储在表变量中。然后我们可以使用cross-apply,因为错误消息说第一个参数需要是字符串文本。不是像'(/X)['+@Count+'].
]这样的表达式-因此,您可以通过动态构造整个SQL字符串并将@Count
连接到中来实现-但是如果您解释您尝试执行的操作的实际最终要求,可能有一种更好的方法,在引号中有多个字符串(“
)虽然@SubhashManikantaKumarMogili。你没有解释你的真正目标是什么,所以我们很难帮助你。
TheXml.value('(/X)[sql:variable("@Count") cast as xs:int?][1]', 'varchar(128)')