SQL XQuery - XML 数据类型方法 "modify" 的参数 1 必须是字符串文字
SQL XQuery - The argument 1 of the XML data type method "modify" must be a string literal
我有以下 SQL 的片段:
declare @idx int = 1
while (@idx <= @NumThresholds)
begin
declare @strIdx nvarchar(100) = convert(varchar(100), @idx)
declare @xqueryString nvarchar(max) = concat('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[', @strIdx, '] with "0"')
set @xml.modify(@xqueryString)
set @idx = @idx + 1
end
我想更新 BlockThreshold
元素的每个值。
但是我收到错误:
The argument 1 of the XML data type method "modify" must be a string literal.
我也试过像这样使用 sql:variable("@strIdx")
:
declare @idx int = 1
while (@idx <= @NumThresholds)
begin
declare @strIdx nvarchar(100) = convert(varchar(100), @idx)
set @xml.modify('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[sql:variable("@strIdx")] with "0"')
set @idx = @idx + 1
end
但是这之后我得到了这个错误:
XQuery [modify()]: Only 'http://www.w3.org/2001/XMLSchema#decimal?',
'http://www.w3.org/2001/XMLSchema#boolean?' or 'node()*' expressions allowed as predicates, found 'xs:string ?'
我只想遍历元素并替换值,但这似乎比我预期的要困难得多。
非常感谢任何帮助!
declare @xml xml = N'
<ElecTariffElements>
<ThresholdMatrix>
<Thresholds>
<BlockThreshold>1</BlockThreshold>
<BlockThreshold>2</BlockThreshold>
<BlockThreshold>3</BlockThreshold>
<BlockThreshold>4</BlockThreshold>
<BlockThreshold>4</BlockThreshold>
</Thresholds>
</ThresholdMatrix>
</ElecTariffElements>
';
declare @idx int = 1;
declare @NumThresholds int = @xml.value('count(//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold)', 'int');
select @NumThresholds, @xml;
while (@idx <= @NumThresholds)
begin
set @xml.modify('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[sql:variable("@idx")][1] with 0');
select @xml;
set @idx = @idx + 1;
end
select @xml;
我有以下 SQL 的片段:
declare @idx int = 1
while (@idx <= @NumThresholds)
begin
declare @strIdx nvarchar(100) = convert(varchar(100), @idx)
declare @xqueryString nvarchar(max) = concat('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[', @strIdx, '] with "0"')
set @xml.modify(@xqueryString)
set @idx = @idx + 1
end
我想更新 BlockThreshold
元素的每个值。
但是我收到错误:
The argument 1 of the XML data type method "modify" must be a string literal.
我也试过像这样使用 sql:variable("@strIdx")
:
declare @idx int = 1
while (@idx <= @NumThresholds)
begin
declare @strIdx nvarchar(100) = convert(varchar(100), @idx)
set @xml.modify('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[sql:variable("@strIdx")] with "0"')
set @idx = @idx + 1
end
但是这之后我得到了这个错误:
XQuery [modify()]: Only 'http://www.w3.org/2001/XMLSchema#decimal?', 'http://www.w3.org/2001/XMLSchema#boolean?' or 'node()*' expressions allowed as predicates, found 'xs:string ?'
我只想遍历元素并替换值,但这似乎比我预期的要困难得多。
非常感谢任何帮助!
declare @xml xml = N'
<ElecTariffElements>
<ThresholdMatrix>
<Thresholds>
<BlockThreshold>1</BlockThreshold>
<BlockThreshold>2</BlockThreshold>
<BlockThreshold>3</BlockThreshold>
<BlockThreshold>4</BlockThreshold>
<BlockThreshold>4</BlockThreshold>
</Thresholds>
</ThresholdMatrix>
</ElecTariffElements>
';
declare @idx int = 1;
declare @NumThresholds int = @xml.value('count(//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold)', 'int');
select @NumThresholds, @xml;
while (@idx <= @NumThresholds)
begin
set @xml.modify('replace value of (//*:ElecTariffElements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold/text())[sql:variable("@idx")][1] with 0');
select @xml;
set @idx = @idx + 1;
end
select @xml;