I have a table from a vendor application that stores some xml data into a column of type varchar(200).
Table structure and sample data is here
declare @table table
(
MerchantID int not null
,Data varchar(200) not null)
insert into @table
select 1, '<product><productID>1</productID><pdesc>ProductDesc</pdesc></product>'
union all
select 2, '<product><itemid>1</itemid><itemname>name of item</itemname></product>'
Is there a way to transform raw xml data into relation format like below in a stored procedure?
for e.g when merchantID passed is 1
MerchantID productID pdesc
1 1 Product Desc
when MerchantID pass is 2 output should be
MerchantID itemid itemname
2 1 name of item
You can use XPath in SQL Server to access XML data nodes.
Here’s an example, using your data.
From there, you should be able to perform your union like so:
if your data is in the same column, you can use a case statement to resolve it.