trying to delete data from two tables and getting error Incorrect syntax near ','.
DELETE tblItem, tblCategory
FROM tblCategory
INNER JOIN tblItem
WHERE tblCategory.category_id = tblItem.fk_category
Update
What's wrong?
trying to delete data from two tables and getting error Incorrect syntax near ','.
DELETE tblItem, tblCategory
FROM tblCategory
INNER JOIN tblItem
WHERE tblCategory.category_id = tblItem.fk_category
Update
What's wrong?
DELETE
can only delete from one table at a time, so you will have to do two separate delete statements, in the correct order:
DELETE
FROM tblItem
WHERE fk_category IN (...)
and then
DELETE
FROM tblCategory
WHERE category_id = (...)
First we can try deleting all items belonging to certain target categories. Then, once those child records have been deleted, we can delete the parent category records.
A better way to handle this is to use something like ON DELETE CASCADE
, which means that if a parent category in tblCategory
were to be deleted, SQL Server would automatically propagate that delete to all child item records in tblItem
.
try like this
DELETE from
FROM tblCategory where category_id in (select fk_category from tblItem )
DELETE from
FROM tblItem where fk_category in (select category_id from tblCategory )