SQL Server XML type

Source: Internet
Author: User

SQL Server supports XML types starting from 2005, which is useful for late changes. A one-to-many relationship turns into a many-to-many relationship at a later stage, and the XML type is a good choice.

  1. Create test data

  Create a table

  -- CREATE table with XML Type column tableint, Info xml   

  Inserting test data

    --Insert 3 test dataINSERT personVALUES (1, ' <person><id>1</id><name> Liu Bei </name></person> ' ) insert person values (2, ' ) insert person values (3, " <person><id>3</id><name> Zhang Fei </name></person>< Span style= "color: #ff0000;" ')                 

  2. Query node contents

Query () method

  -- Querying node Content query () method SELECT id,info.query ('(/person/name) [1]2    

  3. Query Value

Value () method

    --Query values value () methodSELECT Id,info.value (‘(/person/name) [1] ",  varchar style= "COLOR: #ff0000;" > ') from person where ID  = 2 select * where Info.value ( '  (/person/name) [1]  ",  varchar style= "COLOR: #ff0000;" > ') =   Zhang Fei  '                 

  4. Whether the query exists

exist () method

  -- query whether there is a exist () method WHERE info.exist ('(/person/name) [1]1    

  5. Query node

Nodes () method

--Query node SELECT T2. Loc.query ('.') ) from the person cross   APPLY info.nodes ('/person/name asT2 (LOC)    

  6. Modify the XML

Modify () method

  1, increase the node

  --Modify (Insert) Add node SET info.modify (' insert <Age>25</Age> into (/person) [1]  ' ) 3       

  2. Deleting nodes

  -- Delete node contents (XQuery knowledge, no text () directly delete node) UPDATE person SET info.modify (' Delete (/person) [1]/age/t Ext ()' ) 3        

SQL Server XML type

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.