tags:

views:

410

answers:

6

I'm puzzle on how to write a store procedure to handle a conditional delete.

I have a table with 4 columns (ID, A, B, C) and the data in the table look like this:

1 Debbie Jones " "(empty string) " " 2 John Jones " " " " 3 James Jones " " " "

Columns A B C hold person name. How would i write a store procedure that will delete Debbie Jones row and not affect the other two rows with B and C column being empty string?

+2  A: 

You need another condition then. If you want to delete Debbie Jones, then you need to indicate that in your condition, either by specifying that column A = "Debbie Jones" or that the ID column is equal to 1.

This assumes, of course, that the values in the ID and A columns are unique and can uniquely identify the row.

casperOne
A: 

You mean DELETE FROM table WHERE A = "Debbie Jones";?

lc
+2  A: 
delete from YourLousyTableName
where A='Debbie'AND B='Jones'

assuming that ID=1 A='Debbie' B='Jones' C=''

JohnIdol
+1  A: 

I may not be understanding your question completely but are you looking for something like this?

delete from Table
where [Name] = 'Debbie Jones';
Andrew Hare
A: 

First of all, your example needs to be formatted correctly in your question (use the code tag!)

Second of all, I assume you're trying to avoid the mistake of deleting by name because there can be duplicate names. In that case, you probably want to delete only by the ID number.

DELETE FROM MyTable WHERE ID=4

|ID|       A      |  B  |  C  |
|==|==============|=====|=====|
| 1| Debbie Jones | " " | " " |
| 2| John Jones   | " " | " " |
| 3| James Jones  | " " | " " |
Joe Philllips
A: 

I don't think you will need something like stored procedure for this type of situation. any how try this,

create procedure prcDelUsingID
@UID <Data Type of the ID column> 
as
delete from <Your Table Name>
where ID=@UID

execute stored procedure as follows,

prcDelUsingID <ID of the Debbie Jones>
Viki
+1 for pointing out that a parameter is probably necessary rather than hard-coding who should be deleted. -1 for implying that simple SQL statements don't necessarily need to be in stored procedures
Tom H.
Hey Tom, guess account balance is zero with that. :) :) I also agree with your view. But I think that is what Jack is searching for... In other hand I think when it comes to DB related application development it's good to use stored procedures. It prevent SQL Injection attacks. what you say ....
Viki