C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






XQuery with XML data in Sql Server 2005


Posted Date: 22 Aug 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Kundan Kumar Sinha       Member Level: Gold
Rating:     Points: 10



We can use XML data in Sql Server 2005. XML data type is added in SQL Server 2005. We can execute the following code in SQl Server 2005 and see the result.



declare @per xml;
set @per = '<Persons>
<Person id="1">
<Name>
<FirstName>Bob</FirstName>
<LastName>Smith</LastName>
</Name>
<Age>52</Age>
</Person>
<Person id="2">
<Name>
<FirstName>Niels</FirstName>
<LastName>Jones</LastName>
</Name>
<Age>28</Age>
</Person>
<Person id="3">
<Name>
<FirstName>Brian</FirstName>
<LastName>Gates</LastName>
</Name>
<Age>30</Age>
</Person>
</Persons>';
select @per.query('for $p in /Persons/Person where $p/Age > 29 return $p/Name');



When looking at the query above you see:
• The for clause binds the input sequence to the $p iterator variable.
• The input sequence is created through the XPath expression “/Persons/Person”
• The where clause is used against the $p variable to filter the sequence.
• The result is passed back to the client by the return clause.
b. Comparing FLWOR to T-SQL, you can compare the for clause and its subsequent XPath expression to the FROM statement in T-SQL. The return clause is similar to the SELECT statement. As with the SQL SELECT statement you can choose what you want returned by further filter the return clause. In this part of the task you are to return only the first name of the persons with an age over 29 years. Change the query to look like


select @per.query('for $p in /Persons/Person
where $p/Age > 29
return $p/Name/FirstName');



Executing the query above should give you a result like the following:


<FirstName>Bob</FirstName>
<FirstName>Brian</FirstName>






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
XQuery with XML data in Sql Server 2005  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Login stored procedures
Previous Resource: How to check whether another SQL DB server is linked or not
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

Help Desk

Contact Us    Privacy Policy    Terms Of Use