Quantcast
Channel: Getting started with SQL Server forum
Viewing all articles
Browse latest Browse all 9243

How to Import data from XML type column and insert them into another table?

$
0
0

Hi all,

   Here is my scenerio, I have a table which contains a column of type XML DATA type.  

Case 1:

    Now I need to import the data from that xml file and need to insert them in the respective columns in the same table.

Case 2: I would like to get data from that xml file and have to insert into another table and can make a foregin key relationship between these two tables.

The below is the sample structure I give.

<Products>
  <Product>
    <SKU>1</SKU>
    <Desc>Book</Desc>
<test>pass</test>
  </Product>
  <Product>
    <SKU>2</SKU>
    <Desc>DVD</Desc>
<test>pass</test>
  </Product>
  <Product>
    <SKU>3</SKU>
    <Desc>Video</Desc>
<test>fail</test>
  </Product>
</Products>

This is the query which am using now, what it does it takes the xml file from the path am giving.. i dont want the path to specify.. i want to take it from the column of xml type. How to do it?

INSERT INTO Products (sku, product_desc) 
SELECT X.product.query('SKU').value('.', 'INT'),
       X.product.query('Desc').value('.', 'VARCHAR(30)')
FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
     BULK 'D:\Products.xml',
     SINGLE_BLOB) AS T(x)
     ) AS T(x)
CROSS APPLY x.nodes('Products/Product') AS X(product);

Instead of giving the path, i need to mention the column name. :)

 Please provide me some queries that will execute successfully.  


Viewing all articles
Browse latest Browse all 9243

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>