.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> DataBase >> Post New Resource Bookmark and Share

 Subscribe to Articles

Querying and Modifying XML Data in SQL Server 2005

Posted By :Lakhan Pal Garg      Posted Date :27/05/2010   Points :25   Category: DataBase    URL: http://lakhangarg.blogspot.com

Querying XML instances stored in an XML column requires parsing binary XML data in the column. Parsing binary XML is much faster than parsing the text form of the XML data.
 


Introduction

Querying XML instances stored in an XML column requires parsing binary XML data in the column. Parsing binary XML is much faster than parsing the text form of the XML data.

Methods used for Querying and Modifying XML

  • The query() method is useful for extracting parts of an XML instance.
  • The value() method extracts a scalar value from an XML instance.
  • The exist() method is useful for existential checks on an XML instance.
  • The nodes() method yields instances of a special XML data type, each of which has its context set to a different node that the XQuery expression evaluates to.
  • The modify() method permits modifying parts of an XML instance, such as adding or deleting subtrees, or replacing scalar values

Sample Code that will explain the above methods

DECLARE @XmlData xml
SET @XmlData='<Library>
<Subject name="ASP.NET">
<Book ID="1">
<Author>Lakhan Pal Garg</Author>
<Title>ASP.NET Tips</Title>
<Price>$100</Price>
</Book>
<Book ID="2">
<Author>Lakhan Pal Garg</Author>
<Title>SQL Server Tips</Title>
<Price>$90</Price>
</Book>
</Subject>
<Subject name="XML">
<Book ID="3">
<Author>Peter</Author>
<Title>XSLT Tutorial</Title>
<Price>$140</Price>
</Book>
<Book ID="4">
<Author>Rihana</Author>
<Title>XML Parsing in SQL Server</Title>
<Price>$120</Price>
</Book>
</Subject>
</Library>'

select R.i.value('@ID', 'varchar(30)') [BookID],
R.i.query('Author').value('.', 'varchar(30)') [Author],
R.i.query('Title').value('.', 'varchar(30)') [Title],
R.i.query('Price').value('.', 'varchar(30)') [Price]

from @XmlData.nodes('/Library/Subject/Book') R(i)


declare @xml xml
set @xml = '<root/>'
select @xml

declare @value varchar(10)
set @value = 'val1'
set @xml.modify('insert <item value="{sql:variable("@value")}" /> into (/root)[1]')
select @xml

set @value = 'val2'
set @xml.modify('replace value of (/root/item/@value)[1] with "val2"')
select @xml

set @value = 'val3'
set @xml.modify('replace value of (/root/item/@value)[1] with sql:variable("@value")')
select @xml





Featured Articles


Design Pattern Interview Questions Part (3)
Software Architecture Interview Questions Part 3 State Pattern, Stratergy pattern,Visitor pattern, Adapter and fly weight ... Read More
Software Architecture Interview Questions Part 4- Design Patterns
(A) Can you explain bridge pattern? (A) Can you explain composite pattern? (I) Can you explain decorator pattern ? (A) Can you explain Façade pattern? (A) Can you explain chain of responsibility ( COR)? (I) Can you explain proxy pattern? (B) Can you explain template pattern? ... Read More
UML Interview Question Part 1
(B) Define UML? (I) Can you explain use case diagrams? (I) Can you explain primary and secondary actors? (I) How does a simple use case look like? (I) Can you explain 'Extend' and 'Include' in use cases? (I) Can you explain class diagrams? (B) How do we represent private, public and protected in class diagrams? (I) what does associations in a class diagram mean? (I) Can you explain aggregation and composition in class diagrams? (A) What are composite structure diagram and reflexive association in class diagrams? ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend