Facebook Twitter Sign in | Join
Write SQL amazingly fast with SQL Prompt
Getting started with SSIS - Part 1: Introduction to SSIS
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.

Jacob's Blog

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Connect your existing Source Control system to SSMS in 5 minutes
Sponsored [Advertise Here]
Learn XSD and XML Schema Collections
beyondrelational.com
This book helps you to learn XML Schema Collections from basic to advanced levels through simple examples and easy to follow walk through labs.

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

SSRS Tutorial - Getting Started with SQL Server Reporting Services
beyondrelational.com
This tutorial will help you to get started with SQL Server Reporting Services (SSRS)
Loading

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

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

 


Jacob Sebastian
1 · 100% · 16868
1
Liked
0
Comments



Submit

     

Subscribe to Notifications

Your Comment


Sign Up or Login to post a comment.

Copyright © Beyondrelational.com