Given a SQL statement:
select id from myTable
where id = -1
How do I iterate through each of the results? I want to perform a specific operation for each result e.g (in pseudocode):
foreach (result from my statment)
{
delete all rows from myOtherTable that has a value of result;
}
How would I write a SQL statement to do this?
N.B. The reason I want to do this is because the table whose row I want to delete is being referenced by other tables. So I thought that if I delete the rows from all that tables that reference this particular row and then delete the row everything will be fine.
You don't need to iterate in SQL, you can write in one statement
DELETE FROM myOtherTable
WHERE myTableId IN (SELECT id FROM myTable WHERE id = -1)
EDIT:
After you complete the first delete you can then delete from the original table
DELETE FROM myTable WHERE id = -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With