SQL Server XML data parsing (1)

Source: Internet
Author: User

For a long time, I want to write a summary of the common methods for SQL Server to parse XML data. The following are some examples. If something is okay, you can refer to the examples and try to implement your own needs.

Example 1: parse data from XML to table Variables

Declare   @ Itemmessage XML
Declare   @ Itemtable   Table (Itemnumber Int   Primary   Key , Itemdescription Nvarchar ( 300 ))
Set   @ Itemmessage = N ' <Itemlist>
<Item>
<Itemnumber> 1 </itemnumber>
<Itemdescription> Xbox 360, great value </itemdescription>
</Item>
<Item>
<Itemnumber> 2 </itemnumber>
<Itemdescription> Windows phone7, come and have a taste </itemdescription>
</Item>
</Itemlist> '
Insert   Into   @ Itemtable  
(
Itemnumber,
Itemdescription
)
Select T. C. Value ( ' (Itemnumber/text () [1] ' , ' Int ' ),
T. C. Value ( ' (Itemdescription/text () [1] ' , ' Nvarchar (300) ' )
From   @ Itemmessage . Nodes ( ' /Itemlist/item ' ) As T (c)
Select Itemnumber,
Itemdescription
From   @ Itemtable

 

Running result:

Example 2: Parse XML data with namespace

Declare   @ Itemmessage XML
Declare   @ Itemtable   Table (Itemnumber Int   Primary   Key , Itemdescription Nvarchar ( 300 ))
Set   @ Itemmessage = N ' <Itemlist xmlns = "http://cd.love.com/SOA">
<Item>
<Itemnumber> 1 </itemnumber>
<Itemdescription> Xbox 360, great value </itemdescription>
</Item>
<Item>
<Itemnumber> 2 </itemnumber>
<Itemdescription> Windows phone7, come and have a taste </itemdescription>
</Item>
</Itemlist> '
; With Xmlnamespaces ( Default   ' Http://cd.love.com/SOA ' )
Insert   Into   @ Itemtable  
(
Itemnumber,
Itemdescription
)
Select T. C. Value ( ' (Itemnumber/text () [1] ' , ' Int ' ),
T. C. Value ( ' (Itemdescription/text () [1] ' , ' Nvarchar (300) ' )
From   @ Itemmessage . Nodes ( ' /Itemlist/item ' ) As T (c)
Select Itemnumber,
Itemdescription
From   @ Itemtable

 

Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.