sql query XML

 

- Extra information query all nodes in UName value equal to "yellow" 
select * from t_UserPayLog where Extra.exist ( ' // UName [. = " Yellow"]') = 1

 

/ * 
SQL xml entry: 
    - by jinjazz 
    --http: //blog.csdn.net/jinjazz 
    
    1, xml: recognize elements, attributes, and values 
    
    2, xpath: addressing language, similar to the windows directory lookup (useless dir command, then went through the wall) 
                
                syntax, the syntax can be combined as a condition: 
                "." said he, ".." means father, "/" means son, "//" represents future generations, 
                "name" expressed by name Find, "@ name" represented by attribute lookup 
                
                "collection [conditions]" represents a subset of taking a set of conditions, the condition may be 
                    values: numeric, last (), last () - digital like 
                    Boolean: position () <digital , @ name = 'condition', name = 'condition' 
                that may be incorporated when calculating a Boolean value: and or 
    
    . 3, XQuery:Xpath query language based on the subject of prospective, sqlserver xquery contains the following functions 
                exist (xpath condition): returns a Boolean value indicating whether the node exists 
                query (xpath condition): Returns a new xml document by the qualified nodes of
                value (xpath conditions, data type): Returns the specified scalar value, the result must be unique conditions XPath 
                nodes (xpath Condition): Returns the row by the qualified nodes of a table of results 
* / 

DECLARE @data XML 
SET Data @ = ' 
<Bookstore> 
<Book category = "the COOKING"> 
  <title lang = "EN"> Everyday Italian </ title> 
  <author> Giada De Laurentiis </ author> 
  <year> 2005 </ year> 
  <. price> 30.00 </ . price> 
</ Book> 
<Book category = "the CHILDREN"> 
  <title lang = "JP"> Harry Potter </ title> 
  <author> J K. Rowling </ author> 
  <year> 2005 </ year> 
  <. price> 29.99 </. price> 
</ Book> 
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author> of Per Bothner </ author> 
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author> Vaidyanathan Nagarajan </ author> 
  <year> 2003 </ year> 
  <. price> 49.99 </. price> 
</ Book> 
<Book category = "the WEB"> 
  <title lang = "CN"> Learning XML </ title> 
  <author> Erik T. Ray </ author> 
  <year> 2003 </ year> 
  <. price> 39.95 </. price> 
</ Book> 
</ child of the bookstore> 
' 

- test sentence if you do not understand the syntax, please refer to the above rules and xpath xquery function description 

the SELECT @ data.exist ( '// Book [@ category = "the WEB"]') 


--1, document 
the SELECT @data 
--2, whether any level present price node 
select @ data.exist ( '// price') 
--3, acquires all nodes book 
SELECT @ data.query ( '// book') 
--4, acquires all nodes contain lang attribute 
select @ data.query ( '// * [ @ lang]')
--5, obtaining a first book node35] '). Value (' (Book / title) [. 1] ',' VARCHAR (max) ') 
--13, equivalent to 12 
SELECT @ data.query ( '// book [. 1]') 
--6, obtaining the first two nodes book
@ data.query SELECT ( '// book [position () <= 2]') 
7 ì, acquires the last book node 
SELECT @ data.query ( '// book [Last ()]') 
--8, acquisition price> All book node 35 
select @ data.query ( '// book [price> 35]') 
--9, obtaining category = "WEB" book all nodes 
select @ data.query ( '// book [ category = @ "the WEB"] ') 
--10, obtaining the title lang = "en" book all nodes 
SELECT @ data.query (' // book / title [@ lang = "en"] ') 
--11 , obtaining the title lang = "en" and price> All book node 35 
SELECT @ data.query ( '// book [./ title [@ lang = "en"] or price> 35]') 
--12, Get the title lang = "en" and price> first book (first) of the title 35 
select @ data.query ( '// book [ ./ title [@ lang = "en"] and price> 35] ') .value ('(book/title)[1]','varchar(max)')
select @data.value('(//book[./title[@lang="en"] and price>35 ]/title)[1]','varchar(max)') 
--14, obtaining the title lang = "en" and price> first book (first) title lang attribute 35 
select @ data.value ( '((// book [@ category = "WEB" and price> 35] / title) [. 1] / @ lang) [. 1] ',' VARCHAR (max) ') 
--15, acquires the title of the first book 
select Tab.Col.value (' (book / title ) [1] ',' varchar (max) ') AS title 
    from data.nodes @ (' Bookstore ') AS the Tab (CoI) 
--16, obtaining a first author of each book 
select Tab.Col.value (' author [1] ',' VARCHAR (max) ') AS title 
    from data.nodes @ (' // book ') AS the Tab (CoI) 
--17, access to all information of all book 
SELECT 
 TCvalue (' title [. 1] ',' VARCHAR (max) ') AS title, 
 the TCvalue('year[1]','int') as year,
 T.C.value('title[1]','varchar(max)')as title, 
 TCvalue ('. price [. 1] ',' a float ') AS. price, 
 the TCvalue('author[1]','varchar(max)') as author1,
 T.C.value('author[2]','varchar(max)') as author2,
 T.C.value('author[3]','varchar(max)') as author3,
 T.C.value('author[4]','varchar(max)') as author4
from @data.nodes('//book') as T(C)
--18、获取不是日语(lang!="jp")且价格大于35的书的所有信息
select
 T.C.value('title[1]','varchar(max)') as title,
 T.C.value('year[1]','int') as year,
 T.C.value('title[1]','varchar(max)')as title,
 T.C.value('price[1]','float') as price,
 T.C.value('author[1]','varchar(max)') as author1,
 T.C.value('author[2]','varchar(max)') as author2,
 T.C.value('author[3]','varchar(max)') as author3,
 T.C.value('author[4]','varchar(max)') as author4
from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)

  

Guess you like

Origin www.cnblogs.com/lb809663396/p/11125551.html