Sql server SQL XQuery—XML数据类型方法的参数1;修改";必须是字符串文字
我有以下SQL代码片段:Sql server SQL XQuery—XML数据类型方法的参数1;修改";必须是字符串文字,sql-server,xquery,string-literals,Sql Server,Xquery,String Literals,我有以下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/*:
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
但是这个然后我得到这个错误:
XQuery[modify()]:仅限'http://www.w3.org/2001/XMLSchema#decimal?',
'http://www.w3.org/2001/XMLSchema#boolean?'或'node()*'表达式允许作为谓词,找到'xs:string'
我只想迭代元素并替换值,但这似乎比我预期的要困难得多
非常感谢您的帮助 declare@xml=N'
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;
1.
2.
3.
4.
4.
';
声明@idx int=1;
声明@NumThresholds int=@xml.value('count(/*:electrariffelements/*:ThresholdMatrix/*:Thresholds/*:BlockThreshold)'int');
选择@NumThresholds,@xml;
而(@idx
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;