Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, I have a table in which ther is a xml type column. Now i need to extract the file from the xml column and insert the data from those to another table.

below is my xml file :

<products>
<product>
<sku>1
<desc>Book
<test>pass


<product>
<sku>2
<desc>DVD
<test>pass

<product>
<sku>3
<desc>Video
<test>fail


the table will look like below structure. Sorry am not able to insert a screen shot of that.

IntCol | XmlCol

321 | <<Products><product>..|


Now from this column i need to extract the file and insert the data in a separate table. Can anyone plz help me with queries and step by step procedure.Thanks in advance.
Posted
Comments
RDBurmon 19-May-14 13:33pm    
insert some example of xml value and the result that you are expecting

1 solution

--Creating a temporary table variable

declare @Temp table
(
  XMLCol xml
)

--insert your xml data

insert into @Temp values
('<products>
  <product>
    <sku>1</sku>
    <desc>Book</desc>
<test>pass</test>
  </product>
</products>
')
insert into @Temp values
('<products>
  <product>
    <sku>2</sku>
    <desc>DVD</desc>
<test>pass</test>
  </product>
</products>
')
insert into @Temp values
('<products>
  <product>
    <sku>3</sku>
    <desc>Video</desc>
<test>fail</test>
  </product>
</products>
')

--selecting the records from the xml column
select X.N.value(N'(sku)[1]', 'nvarchar(max)') as _sku,
       X.N.value(N'(desc)[1]', 'nvarchar(max)') as _desc,
       X.N.value(N'(test)[1]', 'nvarchar(max)') as _test
from @Temp as T
  cross apply T.XMLCol.nodes(N'/products/product') as X(N)
 
Share this answer
 
Comments
LEBON Iniyavan 22-May-14 0:40am    
Thank you ! Ravi Shankar Dokka.. I got the solution.. similar to yours

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900