gpt4 book ai didi

sql-server - SQL Server 使用游标查询 xml

转载 作者:行者123 更新时间:2023-12-05 00:01:48 27 4
gpt4 key购买 nike

我目前有以下 SQL 2005 代码块。我需要做的是将 XML 作为批量操作导入,但是对于每条记录我可能需要做一些额外的处理(可能插入到一个单独的表中)。目前我只能查询第一项,如何在查看每个 DTO 的游标样式中查询完整数据?

DECLARE @open_xml XML
SET @open_xml = '<DataDTOs>
</pre>
< DataDTO>
< UserId>123456789</UserId>
< ItemID>0</ItemID>
< /DataDTO>
< DataDTO>
< UserId>112456789</UserId>
< ItemID>10</ItemID>
</ DataDTO>
< DataDTO>
< UserId>123456129</UserId>
< ItemID>20</ItemID>
</ DataDTO>
< DataDTO>
< UserId>120056789</UserId>
< ItemID>444</ItemID>
< /DataDTO>
</ DataDTOs>'

DECLARE @userid nvarchar(255)
SELECT @userid =
tab.col.value('UserId[1]','VARCHAR(20)')
FROM @open_xml.nodes('//DataDTO') tab(col)
select @userid

-- Do some stuff

-- Get next UserID

-- Do some stuff

在这方面的任何帮助都会很棒!

谢谢

最佳答案

没什么特别的,只是在@xml 的选择上声明一个光标:

DECLARE @userid nvarchar(255);

DECLARE crsDTO cursor static forward_only read_only for
SELECT
tab.col.value('UserId[1]','VARCHAR(20)')
FROM @open_xml.nodes('//DataDTO') tab(col)

open crsDTO;
fetch next from crsDTO into @userid;
while 0 = @@fetch_status
begin
-- do you work
fetch next from crsDTO into @userid;
end
close crsDTO;
deallocate crsDTO;

通常的警告适用,也许您可​​以将插入作为集合操作而不是游标操作等等。

关于sql-server - SQL Server 使用游标查询 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1595421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com