Detailed description of the example code for SQL operations on Xml fields (Figure)

Source: Internet
Author: User
Tags how to use sql xquery
SQLServer2005 introduces a local data type called XML. You can create a table with one or more XML columns in addition to the relational columns. variables and parameters are also allowed. To better support XML model features (such as document order and recursive structure), XML values are stored as large binary objects (BLOB) in an internal format ).

T-SQLOperate Xml data

I. Preface

SQL Server 2005 introduces a local data type called XML. You can create a table with one or more XML columns in addition to the relational columns. variables and parameters are also allowed. To better support XML model features (such as document order and recursive structure), XML values are stored as large binary objects (BLOB) in an internal format ).

You can use the XML string when saving an XML data to the database. SQL Server automatically converts the string to the XML type and stores it in the database.

With SQL Server's support for XML fields, corresponding, T-SQL statements also provide a large number of XML operations to cooperate with the use of XML fields in SQL Server. This document describes how to use SQL statements to operate on XML.

II. define XML fields

During database design, we can easily define a field as the XML type in the table designer. Note that XML fields cannot be used as primary keys or index keys. Similarly, we can use SQL statements to create data tables that use XML Fields. the following statement creates a table named "docs, this strap has an integer primary key "pk" and a non-typed XML column "xCol ":

CREATE TABLE docs (pk INT PRIMARY KEY, xCol XML not null)

In addition to the table, the XML type can also appear in stored procedures, transactions, functions, and so on. Next we will complete the first step of XML operations. use an SQL statement to define an XML-type data and assign values to it:

declare @xmlDoc xml;set @xmlDoc='
 
  C ProgramDavid
  
   21
  
 '

III. query operations

After defining an XML data type, we usually use query operations. The following describes how to use SQL statements to perform query operations.

In T-SQL, two functions are provided for querying XML data, namely query (xquery) and value (xquery, dataType) the data with tags is obtained, while the value (xquery, dataType) is the tag content. The following classes use these two functions for query.

1Query (xquery)

We need to obtain the title of the book and use query (xquery) for query. the query statement is:

select @xmlDoc.query('/book/title')

Running result

4, Query using xpath

Xpath is a unified Xml query statement supported by. net. You can use XPath to obtain the desired node without using the where statement. For example, we added another node to @ xmlDoc and redefined it as follows:

set @xmlDoc='
 
  
   C# ProgramJerry
   
    50
   
  
  
   Java ProgramTom
   
    49
   
  
 '

-- Get the book node with id 0002

select @xmlDoc.query('(/root/book[@id="0002"])')

The preceding statement can be run independently, and the node with id 0002 is obtained. The running result is as follows:

IV. modification operations

SQL modification operations include update and delete. SQL provides the modify () method to modify Xml. The parameter of the modify method is the XML modification language. The XML modification language is similar to SQL Insert, Delete, and UpDate, but not the same.

1Modify node values

We want to change the price of a book with the id of 0001 to 100 so that we can use the modify method. The code is as follows:

Set @ xmlDoc. modify ('replace value of (/root/book [@ id = 0001]/price/text () [1] with "100 "') -- get the select @ xmlDoc node with the id of 0001. query ('(/root/book [@ id = "0001"])')

Note: The modify method must appear after the set. Running result

2Delete nodes

Next, we will delete the node with id 0002. the code is as follows:

-- Delete the book node set @ xmlDoc. modify ('Delete/root/book [@ id = 0002] ') select @ xmlDoc

Running result

3Add node

In many cases, we also need to add nodes to the xml file. in this case, we also need to use the modify method. Next, we will add an ISBN node to the book node with id 0001. the code is as follows:

-- Add a node

set @xmlDoc.modify('insert 
 
  78-596-134
  before (/root/book[@id=0001]/price)[1]')select @xmlDoc.query('(/root/book[@id="0001"]/isbn)')

Running result

4, Add and delete attributes

After you learn how to operate nodes, you will find that, in many cases, we need to operate nodes. At this time, we still use the modify method. for example, we add a date attribute to the book node with id 0001 to store the publishing time. The code is as follows:

-- Add attributes

set @xmlDoc.modify('insert attribute date{"2008-11-27"} into (/root/book[@id=0001])[1]')select @xmlDoc.query('(/root/book[@id="0001"])')

Running result

If you want to add multiple attributes to a node at the same time, you can use a set of attributes. The set of attributes can be written as: (attribute date "}, attribute year {"2008"}), you can add more. I will not give an example here.

5, Delete attributes

Delete an attribute. for example, you can use the following code to delete the id attribute of the book node whose id is 0001:

-- Delete attributes

set @xmlDoc.modify('delete root/book[@id="0001"]/@id')select @xmlDoc.query('(/root/book)[1]')

Running result

6Modify attributes

Modifying the attribute value is also very common. for example, to change the id attribute of the book node whose id is 0001 to 0005, we can use the following code:

-- Modify attributes

set @xmlDoc.modify('replace value of (root/book[@id="0001"]/@id)[1] with "0005"')select @xmlDoc.query('(/root/book)[1]')

Running result

OK. after the above learning, I believe you can use the Xml type in SQL. below is what we didn't mention. you can check the exist () method elsewhere, used to determine whether a specified node exists. The return value is true or false. the nodes () method, it is used to convert a group of nodes returned by a query into a group of record rows in a table similar to a result set.

The above is a detailed description of the SQL operation sample code for Xml fields (figure). For more information, see other related articles in the first PHP community!

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.