I have a table "Blah" with a PK column BlahID and an XML column BlahItems in a database on SQL Server 2005.
This table has records as follows...
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 Fred 1 Wilma 2 Lisa 2 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 Fred 1 Wilma
My ultimate goal is to create a VIEW of the "expanded" data and then query against the view.