Use the openxml function to convert an XML document to a row result set.

Source: Internet
Author: User

The for XML clause converts a row result set to an XML result set. to convert an XML document to a row result set, use the openxml function. Openxml is provided in SQL Server 2000, but is enhanced in SQL Server 2005.
Openxml Syntax:
The running result is:
Firstname middlename lastname
------------------------------------------------
Gustavo null achong
Catherine R. Abel
If the third parameter of the openxml function in the Code is changed to 2, two rows of null value will be returned, because 2 indicates that the query is element-centric, and no other elements under the row node. In the same way, if the given XML document has only elements but no attributes, you must use parameter 2 instead of 1. If you want to query a part of the data in the element attributes and a part in the element sub-element, you can replace this parameter with 3. For the query statements and returned results, see the code:

Openxml (IDOC int [in], rowpattern nvarchar [in], [flags byte [in])
[With (schemadeclaration | tablename)]

The first parameter IDOC is the handle of the XML document, which must be obtained by calling sp_xml_preparedocument to create an internal table form of the XML document. The rowpattern parameter is an XPATH pattern used to identify the node to be processed. In the third parameter, 1 indicates that the query is Attribute-centric, and 2 indicates that the query is element-centric. Finally, the with clause identifies the fields to be returned.
Use the openxml function to convert the XML document to the code of the row result set:

Declare @ mydoc XML
Set @ mydoc ='
<Person>
<Row firstname = "Gustavo" lastname = "achong"/>
<Row firstname = "Catherine" middlename = "R." lastname = "Abel"/>
</Person>
'-- Define the XML document
Declare @ dochandle int
Exec sp_xml_preparedocument @ dochandle output, @ mydoc
-- Get the XML document handle
Select * From openxml (@ dochandle, '/person/row', 1) -- 1 indicates Attribute-centric
With (firstname nvarchar (50), middlename nvarchar (50), lastname nvarchar (50 ))

 

Declare @ mydoc XML
Set @ mydoc ='
<Products>
<Product category = "book">
<Name> Windows 2008 </Name>
<Vendor> vendor1 </vendor>
</Product>
<Product category = "book">
<Name> sql2008 </Name>
<Vendor> vendor2 </vendor>
</Product>
</Products>'
Declare @ dochandle int
Exec sp_xml_preparedocument @ dochandle output, @ mydoc
Select * From openxml (@ dochandle, '/products/product', 3)
With (Category nvarchar (50), name nvarchar (50), Vendor nvarchar (50 ))

In fact, no error is reported for the third parameter and any positive integer SQL Server 2005. Microsoft official msdn only provides the meaning of the four numbers 0, 1, 2, and 8. In fact, SQL Server 2005 determines the query method based on the value in the binary bit of the third parameter. The last two digits are 00 or 01 (for example, 0, 1, 4, and 5). The query is based on the attribute and the last two digits are 10 (for example: 2, 6, 10, etc.) is an element-centric query, and the last two digits are 11 (for example, 3, 7, etc.) indicates that both the query attribute and the query element are required.

 

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.