Five basic operations on XML data in SQL Server

Source: Internet
Author: User
Tags xquery

1. xml. exist
If the input is an XQuery expression, 0, 1 or null is returned. 0 indicates no, 1 indicates yes, and null indicates that the input is null.
2. xml. Value
The input is an XQuery expression and returns an SQL Server scalar value.
3. xml. Query
The input is an XQuery expression and returns an SQL Server XML stream.
4. xml. Nodes
The input is an XQuery expression and returns a column set of XML documents.
5. xml. Modify
Use an XQuery expression to insert, update, and delete XML nodes.

The following are examples of the above five operations:

Declare @ xmlvar xml ='

<Catalog>

<Book category = "itpro">

<Title> Windows step by step </title>

<Author> Bill Zack </author>

<Price> 49.99 </price>

</Book>

<Book category = "Developer">

<Title> developing ADO. Net </title>

<Author> Andrew brust </author>

<Price> 39.93 </price>

</Book>

<Book category = "itpro">

<Title> Windows Cluster Server </title>

<Author> Stephen forte </author>

<Price> 59.99 </price>

</Book>

</CATALOG>'

1. xml. exist

Select @ xmlvar. exist ('/CATALOG/Book') ----- return 1

Select @ xmlvar. exist ('/CATALOG/book/@ category') ----- return 1

Select @ xmlvar. exist ('/CATALOG/book1') ----- 0 is returned.

Set @ xmlvar = NULL

Select @ xmlvar. exist ('/CATALOG/Book') ----- return null

2. xml. Value

Select @ xmlvar. Value ('/CATALOG [1]/book [1]', 'varchar (max )')

Select @ xmlvar. Value ('/CATALOG [1]/book [2]/@ category', 'varchar (max )')

Select @ xmlvar. Value ('/CATALOG [2]/book [1]', 'varchar (max )')

Result set:

Windows step by stepbill zack49.99 developer null

3. xml. Query

Select @ xmlvar. Query ('/CATALOG [1]/Book ')

Select @ xmlvar. Query ('/CATALOG [1]/book [1]')

Select @ xmlvar. Query ('/CATALOG [1]/book [2]/author ')

The result sets are:
<Book category = "itpro">
<Title> Windows step by step </title>
<Author> Bill Zack </author>
<Price> 49.99 </price>
</Book>
<Book category = "Developer">
<Title> developing ADO. Net </title>
<Author> Andrew brust </author>
<Price> 39.93 </price>
</Book>
<Book category = "itpro">
<Title> Windows Cluster Server </title>
<Author> Stephen forte </author>
<Price> 59.99 </price>
</Book>

<Book category = "itpro">
<Title> Windows step by step </title>
<Author> Bill Zack </author>
<Price> 49.99 </price>
</Book>

<Author> Andrew brust </author>

4. xml. Nodes

Select T. C. Query ('.') as result from @ xmlvar. nodes ('/CATALOG/Book') as T (c)

Select T. C. Query ('title') as result from @ xmlvar. nodes ('/CATALOG/Book') as T (c)

The result sets are:
<Book category = "itpro"> <title> Windows step by step </title> <author> Bill ............

<Book category = "Developer"> <title> developing ADO. Net </title> <author> Andrew ............
<Book category = "itpro"> <title> Windows Cluster Server </title> <author> Stephen ............

<Title> Windows step by step </title>
<Title> developing ADO. Net </title>
<Title> Windows Cluster Server </title>

5. xml. Modify

For more information about modify, see

Http://blog.csdn.net/tjvictor/archive/2009/07/21/4368496.aspx

 

Original column from csdn tjvictor

Http://blog.csdn.net/tjvictor/archive/2009/07/21/4368511.aspx

 

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.