gilliduck
gilliduck

Reputation: 2933

Delete with Case/If Statement

I have a table

Table1
+-------+-----------+----------+
| EmpNo | FirstName | LastName |
+-------+-----------+----------+
|   123 | Bob       | Smith    |
|   456 | John      | Smith    |
|   789 | Bill      | Smith    |
+-------+-----------+----------+

I would like to make a stored procedure to either delete the entire table or just one entry from the table based on a parameter passed in.

Something similar to

Delete from Table1
Case where @DeleteAll = 1
    Then where EmpNo is not null
Else where EmpNo = @employee

or

delete from Table1
If @DeleteAll = 1
    Then where EmpNo is not null
Else
    where EmpNo = @employee
select * from Table1

Upvotes: 0

Views: 129

Answers (2)

Aaron Dietz
Aaron Dietz

Reputation: 10277

Try this:

IF @DeleteAll = 1
BEGIN
   DELETE 
   FROM TABLE1
   WHERE EmpNo IS NOT NULL
END
ELSE
   DELETE
   FROM TABLE1
   WHERE EmpNo = @employee
;

Upvotes: 1

juergen d
juergen d

Reputation: 204854

Delete from Table1
where @DeleteAll = 1
   or EmpNo = @employee

Upvotes: 3

Related Questions