Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
XQuery 69
TSQL 67
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
August 2009 19
June 2009 19
May 2010 18
January 2009 15
July 2008 15
January 2010 14
October 2008 14
February 2010 12

XQuery Lab 49 – Deleting rows from a table based on the data in an XML document

Aug 26 2009 11:24AM by Jacob Sebastian   

We have seen a few examples that demonstrated how to delete elements and attributes from XML documents. In this lab, let us see how to delete rows from a table, based on the data in an XML document.

DECLARE @t TABLE (id INT)
INSERT INTO @t(id)
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3

SELECT * FROM @t
/*
id
-----------
1
2
3
*/

declare @XmlData xml
set @XmlData = '
<PersonalInformationObject>
<Skills>
<SkillObject>
<SkillId>1</SkillId>
</SkillObject>
<SkillObject>
<SkillId>2</SkillId>
</SkillObject>
</Skills>
</PersonalInformationObject>'


DELETE t
FROM @t t
CROSS APPLY @XmlData.nodes('
/PersonalInformationObject/Skills/SkillObject/SkillId
[. = sql:column("id")]'
)
a(x)

SELECT * FROM @t
/*
id
-----------
3
*/

Note the usage of CROSS APPLY and the way the join is established between the table and the XML document using the sql:column() function within the XQuery expression.

Previous Lab: XQuery Lab 48 - Sorting Query files in SQL Server Management Studio (SSMS) Solution/Project

View All Labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials

 

Tags: XQuery-Functions, XQuery-Labs, XML, XQuery, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 22473
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising