I have two tables in the database and I would like to delete on cascade with a trigger. I use to use a cascade deleting in the relationship, but in this case I have more tables and SQL Server does not let me to set cascde deleting in the relationship because it would create a cycle.
So I want to create a trigger to delete the registers of the table that I can't set cascade deleting in the relationship.
I use this trigger:
CREATE TRIGGER trg_myTrigger
ON Componentes
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
Delete from mytable2 where IDParentFromTable1 IN(select deleted.IDtable1 from deleted)
END
GO
However I get and error of foreign key.
really I don't know why I get this error.
Thanks.