Wednesday, 18 January 2017

Convert XML column to table

How we can convert XML column in to table.



 Declare @xml xml
 set @xml=convert
(xml,
N'<ROOT>
<CCS ClientId="484" ChannelId="7" Profit="0" Asb="0.0001" OnlineResultsSSRSReportPath="C:/This"/>
<CCS ClientId="484" ChannelId="11" Profit="1000" Asb="100"/>
<CCS ClientId="484" ChannelId="5"/>
</ROOT>'
)

;WITH CT
AS
( SELECT
x.p.value('(@ClientId)[1]', 'int') AS ClientId
,x.p.value('(@ChannelId)[1]', 'int') AS ChannelId
,x.p.value('(@Profit)[1]', 'float') AS Profit
,x.p.value('(@Asb)[1]', 'float') AS Asb
,x.p.value('(@OnlineResultsSSRSReportPath)[1]', 'varchar(500)') AS OnlineResultsSSRSReportPath
FROM @xml.nodes('/ROOT/CCS') x(p)
)
  select * from CT