sql server - why I get an error with the trigger when I try to delete on cascade? -
i have 2 tables in database , delete on cascade trigger. use use cascade deleting in relationship, in case have more tables , sql server not let me set cascde deleting in relationship because create cycle.
so want create trigger delete registers of table can't set cascade deleting in relationship.
i use trigger:
create trigger trg_mytrigger on componentes after delete begin -- set nocount on added prevent result sets -- interfering select statements. set nocount on; -- insert statements trigger here delete mytable2 idparentfromtable1 in(select deleted.idtable1 deleted) end go
however , error of foreign key.
really don't know why error.
thanks.
the trigger fires after delete, foreign key error happens before trigger runs.
you need use 1 of
- a stored proceure delete mytable2 componentes
- an instead of (before trigger)
- two separate deletes in transaction
Comments
Post a Comment