Question : Passing XML to a stored procedure to delete multiple records from a SQL Server table

I used the following article to do multiple inserts on a table by passing xml to a stored proc...

http://weblogs.sqlteam.com/travisl/archive/2005/01/04/3931.aspx

The technique works great but now I would like to delete items for a table called Items.  It has two fields  - ItemID int and EmpID int

I am passing in XML that looks like this representing what rows need to be deleted...

           
           
           
           
           
           


What is the best way to do this.  I just want to pass the XML into the stored proc and have all the items in the XML to be deleted in the table..

Please include sql code for credit.

Thanks in advance!


Answer : Passing XML to a stored procedure to delete multiple records from a SQL Server table

Two comments on the previous code:  
1. If there is any chance the Xml document could be larger than 8000 characters consider using text or if you are using SQL Server 2005 varchar(MAX).

2. There is no need to use a CURSOR for something as simple as this.  You can do something like this:

Create Procedure usp_DeleteItems
                                 @XmlDoc text
AS

Declare @idoc int

SET NOCOUNT ON
EXEC sp_xml_preparedocument @iDoc OUTPUT, @XmlDoc

Delete      i
From      Items i
            Inner Join (
                  SELECT      ItemID, EmpID
                  From      OPENXML(@iDoc, '/items/item', 1) WITH (
                              ItemID int '@itemid',
                              EmpID int '@empid')) x On i.ItemID = x.ItemID And i.EmpID = x.EmpID      -- Assuming that both ItemID and EmpID are required

EXEC sp_xml_removedocument @iDoc
Random Solutions  
 
programming4us programming4us