0

How can I delete a row in an sql table without previously knowing if it exist in that table or no

 delete from table id=4 

for example I want to delete an id equal to 4 but I don't know if the id having the number 4 exist or no in this table should I use a procedure to check if it exist or can I just use a simple delete statement

Sora
  • 2,465
  • 18
  • 73
  • 146

4 Answers4

1
DELETE FROM tableName WHERE id=4;
Vucko
  • 20,555
  • 10
  • 56
  • 107
1

Yes, you can use a simple delete statement to try to delete the record.

If the record exists in the table, it will be deleted. If it does not exist, then nothing will happen.

delete 
from yourTable
where id=4 
Taryn
  • 242,637
  • 56
  • 362
  • 405
0

Just do it. The syntax is like this:

DELETE FROM tableName WHERE id=4;

The number of rows deleted will be stored in @@ROWCOUNT. You will not get an exception if no rows are deleted, but @@ROWCOUNT will be 0.

You can find an example here. The example is an UPDATE statement, but DELETE works the same way.

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

do like this: DELETE FROM tableName WHERE id=4;

if there is a row exist which id= 4 than it will delete otherwise nothing will happen

Vaimin
  • 7
  • 1
  • 6