Friday, January 6, 2012

XML columns/variables


CREATE TABLE [dbo].[SampleXmlTable](
[GroupID] [int] NULL,
[Books] [xml] NULL
) ON [PRIMARY]


--select a node

SELECT
GroupID,
Books.value('(catalog/book/author)[1]','nvarchar(200)') as Author,
Books.value('(catalog/book/price)[2]','nvarchar(200)') as Price
FROM
[dbo].[SampleXmlTable]

GroupID Author Price
1 Gambardella, Matthew 5.95
1 Kress, Peter 36.95

--expand

declare @SampleXml Xml




























SELECT
    DataTable.books.value('author[1]','nvarchar(100)') AS author,
DataTable.books.value('title[1]','nvarchar(100)') AS title,
DataTable.books.value('price[1]','float') AS price
FROM
@SampleXml.nodes('//catalog/book') AS DataTable(books)

No comments: