通过 xml 循环,然后更新 SQL 中的 xml 变量
Looping thru xml then updating xml variable in SQL
我的 xml 看起来像 ff:
<root>
<TemplateQuestion>
<Row rfqID="1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>
现在我的目标是让 rfqID 前面有字母 "q"。所以结果应该像 ff:
<root>
<TemplateQuestion>
<Row rfqID="q1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="q2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="q3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>
我通过这样做实现了这一目标:
declare @xml XML
set @xml = (select dbo.udfGetXMLVal(1))
declare @nodeCount int
declare @i int
declare @qid nvarchar(20)
set @i = 1
select @nodeCount = @xml.value('count(/root/TemplateQuestion/Row/@rfqID)','int')
while(@i <= @nodeCount)
begin
select @qid = x.value('@rfqID[1]', 'VARCHAR(20)')
from @xml.nodes('/root/TemplateQuestion/Row[position()=sql:variable("@i")]') e(x)
set @qid = 'q' + @qid
select @qid
Set @xml.modify('replace value of (/root/TemplateQuestion/Row/@rfqID)[1] with sql:variable("@qid")')
set @i = @i + 1
end
我在使用这条线时遇到问题:
Set @xml.modify('replace value of (/root/TemplateQuestion/Row/@rfqID)[1] with sql:variable("@qid")')
如何将[1]替换为变量@i?当我尝试使用 sql:variable.
时,字符串文字出现一些错误
如果您能提供任何帮助,我们将不胜感激。谢谢
快速而肮脏:-)
SELECT CAST(REPLACE(CAST(@x AS VARCHAR(MAX)),' rftID="',' rftID="q') AS XML);
这是一个干净的方法:
DECLARE @x XML='<root>
<TemplateQuestion>
<Row rfqID="1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>';
SELECT
(
SELECT 'q' + R.value('@rfqID','varchar(max)') AS [@rfqID]
,R.value('@rftID','int') AS [@rftID]
,R.value('@questionDesc','varchar(max)') AS [@questionDesc]
--other attributes similar
FROM @x.nodes('/root/TemplateQuestion/Row') AS A(R)
FOR XML PATH('Row'),ROOT('TemplateQuestion'),TYPE
)
FOR XML PATH('root');
"How can I replace the [1]
to the variable @i
? I get some error with string literals when I try to use sql:variable
"
你可以这样做(在 SQL Server 2008R2 中测试和工作):
Set @xml.modify('
replace value of ((/root/TemplateQuestion/Row/@rfqID)[sql:variable("@i")] )[1]
with sql:variable("@qid")
')
我的 xml 看起来像 ff:
<root>
<TemplateQuestion>
<Row rfqID="1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>
现在我的目标是让 rfqID 前面有字母 "q"。所以结果应该像 ff:
<root>
<TemplateQuestion>
<Row rfqID="q1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="q2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="q3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>
我通过这样做实现了这一目标:
declare @xml XML
set @xml = (select dbo.udfGetXMLVal(1))
declare @nodeCount int
declare @i int
declare @qid nvarchar(20)
set @i = 1
select @nodeCount = @xml.value('count(/root/TemplateQuestion/Row/@rfqID)','int')
while(@i <= @nodeCount)
begin
select @qid = x.value('@rfqID[1]', 'VARCHAR(20)')
from @xml.nodes('/root/TemplateQuestion/Row[position()=sql:variable("@i")]') e(x)
set @qid = 'q' + @qid
select @qid
Set @xml.modify('replace value of (/root/TemplateQuestion/Row/@rfqID)[1] with sql:variable("@qid")')
set @i = @i + 1
end
我在使用这条线时遇到问题:
Set @xml.modify('replace value of (/root/TemplateQuestion/Row/@rfqID)[1] with sql:variable("@qid")')
如何将[1]替换为变量@i?当我尝试使用 sql:variable.
时,字符串文字出现一些错误如果您能提供任何帮助,我们将不胜感激。谢谢
快速而肮脏:-)
SELECT CAST(REPLACE(CAST(@x AS VARCHAR(MAX)),' rftID="',' rftID="q') AS XML);
这是一个干净的方法:
DECLARE @x XML='<root>
<TemplateQuestion>
<Row rfqID="1" rftID="1" questionDesc="Question 1" responseType="1" rfqDisplayOrder="1" deletedBit="0" />
<Row rfqID="2" rftID="1" questionDesc="Question 2" responseType="2" rfqDisplayOrder="2" deletedBit="0" />
<Row rfqID="3" rftID="1" questionDesc="Question 3" responseType="3" rfqDisplayOrder="3" deletedBit="0" />
</TemplateQuestion>
</root>';
SELECT
(
SELECT 'q' + R.value('@rfqID','varchar(max)') AS [@rfqID]
,R.value('@rftID','int') AS [@rftID]
,R.value('@questionDesc','varchar(max)') AS [@questionDesc]
--other attributes similar
FROM @x.nodes('/root/TemplateQuestion/Row') AS A(R)
FOR XML PATH('Row'),ROOT('TemplateQuestion'),TYPE
)
FOR XML PATH('root');
"How can I replace the
[1]
to the variable@i
? I get some error with string literals when I try to usesql:variable
"
你可以这样做(在 SQL Server 2008R2 中测试和工作):
Set @xml.modify('
replace value of ((/root/TemplateQuestion/Row/@rfqID)[sql:variable("@i")] )[1]
with sql:variable("@qid")
')