Five basic operations on SQL Server XML data

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 the next article.
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.