XQuery method sample in SQL Server 2005

SQL Server - T-SQL , Posted at : Mar/29/2007  
2649 Views   1 Comments

As we already know that SQL Server 2005 now supported the xml data type. In the previous version of SQL Server we can use the FOR XML clause to retrieve the data in order to view in xml. Some new features has been added to this xml data type, some are listed below :

  • Support for xml schema. We can validate the data against the xml column when inserting new row by xml schema. Its affect to the kind of xml column type : typed xml (xml schema included), untyped xml (no xml schema included).
  • The ability to use XQuery against xml data stored in xml column or variables
  • Ability to index xml columns
  • Support XML-DML
  • Other improvement to the existing SQL Server 2000 XML functionality

In this article i'll show you how can we use some XQuery method to query the xml data which stored in xml data type variable. The methods are : query(),  exist(). Let us consider the following code :

Declare @data xml
Set @data =
'
<books>
<book>
<title>Sql Server 2005</title>
</book>
<book>
<title>C# 2005</title>
</book>
</books>
'

--query the data to retrieve the record from the book node :
Select @data.query('books/book')

the result is :

<book><title>Sql Server 2005</title></book><book><title>C# 2005</title></book>

if we want to eliminate the book element we have to change the query :

Select @data.query('books/book/title')

the result is :

<title>Sql Server 2005</title><title>C# 2005</title>

Now, how can we retrieve the data without the title element? We can add the data() function to the query paramater :

Select @data.query('data(books/book/title)')

the result is :

Sql Server 2005 C# 2005

In order to find some existing data based on the criteria input by user, Sql server has the exist() method that combine with the text() paramater added to query. I'll modify the the script above :

Declare @data xml, @exist bit
Set @data =
'
<books>
<book>
<title>Sql Server 2005</title>
</book>
<book>
<title>C# 2005</title>
</book>
</books>
'

Set @exist = @data.exist('books/book/title[text()="Sql Server 2005"]')
if (@exist = 0)
Begin
Select 'No such record'
End
else
Begin
Select 'Record(s) found'
end

the result is :

Record(s) found

By now you can see some benefits from the xml data type supported in SQL Server 2005.


[Comments]
irvan  Dec/15/2009 11:16:18 :
Keren,,thx for article,, Sekarangg sy lagi mendalami SQL SERVER 2005 yang beberapa feature barunya kurang sy pahami,, mungkin mas rully bisa membantu,, misalnya service broker, atau notification ,, mungkin mas rully bisa menerangkannya dengan memberikan contoh2,,, Bisa di email ke vanblazs@yahoo.com,,thx

[Write your comment]
Name (required)
Email (required-will not published)
 
Comment

pfog
Input code above below (Case Sensitive) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 3175 times  
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 2592 times  
Mapping Hak Akses User Pada MenuStrip Sampai Control Button : Downloaded 2180 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 2127 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 1806 times  
Article Category 
Links 
Award 
Certifications 
MOS 2007
MCAS
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Native Enterprise 
Follow Me 
Facebook   LinkedIn   Twitter
Syndication 
Hosted By 
Native Enterprise News