gpt4 book ai didi

xml - 如何将 XML 列连接回它的来源记录?

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:57 25 4
gpt4 key购买 nike

我在 SQL Server 2005 的数据库中有一个表“Blah”,其中包含一个 PK 列 BlahID 和一个 XML 列 BlahItems。

这个表有如下记录...

BlahID BlahItems------ ------------------------------------------------------1      <root><item name="Fred" /><item name="Wilma" /></root>2      <root><item name="Lisa" /><item name="Bart" /></root>

How can I query that table to produce the following....

BlahID BlahItem------ --------1      Fred1      Wilma2      Lisa2      Bart

The closest I've managed to get is on a per record FUNCTION that does something along the lines of the following...

CREATE FUNCTION dbo.Blahs(@id int)
RETURNS @list TABLE (BlahID int, BlahItem nvarchar(max))
BEGIN
DECLARE @xml AS xml
SELECT @xml = BlahItems FROM dbo.Blah AS b WHERE b.BlahID = @id

INSERT INTO @list
SELECT @id, tbl.col.value('@name','nvarchar(max)')
FROM @xml.nodes('/root/item') tbl(col)
RETURN
END

SELECT * FROM dbo.Blahs(1)
BlahID BlahItem------ --------1      Fred1      Wilma

我的最终目标是创建“扩展”数据的 View ,然后对该 View 进行查询。

最佳答案

你可以试试这个查询:

SELECT BlahID, XmlItems.BlahItem.value('@name', 'nvarchar(100)') AS BlahItem
FROM Blah CROSS APPLY BlahItems.nodes('/root/item') AS XmlItems(BlahItem)

更多信息请查看this three-part article亚历克斯荷马。

关于xml - 如何将 XML 列连接回它的来源记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/266556/

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