Sharpeye500
Sharpeye500

Reputation: 9073

Delete parameter & returning back unsuccsessful Ids

Is this possible?

I get comma separated IDs as a integer/string arrays as a param (ie. 100,101,102,103,104,105).

I need to pass into in a delete query.

ie. delete from tablename where ID in (100,101,102,103,104,105) and status='Completed';

If 100,102,105 gets deleted & if 101 & 103 don't get deleted (for some error), then i need to return back 101 & 103 not deleted with error message(if status is not completed).

Pls. throw some lights on this.

Upvotes: 0

Views: 77

Answers (3)

Tomas Jansson
Tomas Jansson

Reputation: 23472

You could probably use a transaction and split into two operations. First you delete everything in the list, then you query for everything in the list and you will get a list of those that weren't deleted. After that just complete the transaction.

EDIT: Not even sure you need a transaction, it depends on your requirements.

Upvotes: 1

user47589
user47589

Reputation:

You could use a SQL stored procedure to accomplish this. Call the sproc using the list of ids, and have the sproc return a table of ids and the reason it did not succeed.

Upvotes: 0

Robaticus
Robaticus

Reputation: 23157

What it comes down to is that you're going to need to process each ID individually and determine whether or not the delete succeeds. As I said in my comment, you have a variety of choices on what you return back to the calling process. It could be a List<CustomClass> a Dictionary<int, string>, or even a list of tuples.

I personally tend to use List<CustomClass>, because when I return two things (e.g. ID and error), I always wind up wanting to return more later.

Upvotes: 0

Related Questions