I have SQL table:
Create table
(
ID varchar(50) not null,
Action nvarchar(max) null
)
Action column contains XML data. Format:
<?xml version="1.0" encoding="UTF-8"?>
<oo_outbound_order>
<oo_master>
<Code>123</Code>
<Name>Branan</Name>
</oo_master>
</oo_outbound_order>
How to parse this column? Result should be like this:
CODE NAME
123 Branan
I have an XML file with a lot of data, and i want to put all that data in in SQL SERVER database, in a table with columns like the last exemple of the article. So, here is my code : GO EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML SELECT @XML = XMLData FROM XMLwithOpenXML
In the Data Flow tab, drag and drop the following components and join them: Double click the OLE DB Source and connect to the table with the XML data type: Using the XML Parser Transform, select the input and write the XML String that you want to parse:
Now as I said before, XML data stored in a column of data type XML can be processed either by using XML functions available in SQL Serveror by using thesp_xml_preparedocumentstored procedure along with the OPENXMLfunction.
Change the column type to XML by using the ALTER TABLE statement. You can bulk copy your data from another SQL Server database by using bcp out, and then bulk insert the data into the later version database by using bcp in.
Try the follwing meathod :
DECLARE @XMLData XML = '
<oo_outbound_order>
<oo_master>
<Code>123</Code>
<Name>Branan</Name>
</oo_master>
</oo_outbound_order>'
SELECT
[Code] = Node.Data.value('Code', 'INT'),
[Name] = Node.Data.value('Name', 'NVARCHAR(20)')
FROM @XMLData.nodes('/oo_outbound_order/oo_master') Node(Data)
There are many tutorial articles about xml parsing with TSQL. For example, http://www.sqlserver.info/syntax/parse-xml-with-sql-server/
DECLARE @xml xml
SET @xml =
'<?xml version="1.0" encoding="UTF-8"?>
<oo_outbound_order>
<oo_master>
<Code>123</Code>
<Name>Branan</Name>
</oo_master>
</oo_outbound_order>'
SELECT
n.value('(./Code/text())[1]','int') as CODE
, n.value('(./Name/text())[1]','Varchar(50)') as NAME
FROM @xml.nodes('/oo_outbound_order/oo_master') as a(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With