I have an table at my SQLSERVER(2008R2) called “Clients” , it has ClientID and Address.
I’d like to make a query/SP which does the following:
- Recieves a list of addresses.
- Deletes the records with these address.
- Returns a list of the addresses that can’t be deleted(row not found, etc).
- Returns the number of deleted rows.
My application is C#-ASP.NET based application.
I’ve thought of the following solutions:
-
Make an
SPthat recieves a list of addresses, and then tries to delete them, and if it can’t delete it , to add it to some sort of array/list. – my problem with this solution is that I’m not familiar with how lists works at SQLSERVER(2008R2). -
Work with
DataTable, select the data I need from the DB, and delete it withSqlDataAdapter, and check whats the rows which have changed viaRowState.
Example of my DataBase:
Client ID Address
111111111 'foo st. 2'
222222222 'foo bld 1'
333333333 'foo rd 22'
444444444 'foo st. 1'
Example of the input:
input list{'foo st. 2','foo bld 1','foo st 22','foo st 1'} – the last two items aren’t exist at the table above, So i would like my SP (or any other method of solution) to return:
'Deleted rows: 2' //thats not a problem
'list of the records which weren't removed: {'foo st 22','foo st 1'} //thats a problem..
Hope I made myself as clear as possible.
I don’t think you can find which items aren’t deleted by a delete statement in sql. Firstly you need a way to split your string by comma’s. I’d use a table function like what’s suggested in this question
once you have that then you need to select before you delete into a temp table, then delete the records that are there, then return the missing records: