Answer the question
In order to leave comments, you need to log in
How to get values from deleted record in MS SQL SERVER?
There are three tables. Two of them are connected to the third one by means of foreign keys.
In the delete trigger, you need to somehow get the value of the primary key of the row being deleted in order to delete rows from the remaining two tables using this primary key. Actually, how to count values from the deleted line?
Answer the question
In order to leave comments, you need to log in
In the trigger the deleted table contains deleted lines (I repeat "terms", but not one line). For example:
CREATE TRIGGER PersonDelete ON [Person] FOR DELETE AS
BEGIN
SET NOCOUNT ON
-- [dbo].[Variant]
DELETE FROM Variant WHERE (Variant.Kind = 'P') AND (Variant.[Object] IN ( SELECT ID FROM deleted))
-- [dbo].[Internet]
DELETE FROM Internet WHERE (Internet.Kind = 'P') AND (Internet.[Object] IN ( SELECT ID FROM deleted))
END
--
GO
CREATE TRIGGER AttributeValueUpdateDelete ON FilmAttributeValue FOR UPDATE, DELETE AS
BEGIN
SET NOCOUNT ON
UPDATE FilmAttributeGroup
SET Uses = (SELECT COUNT(*) FROM FilmAttributeValue WHERE ([Group] = deleted.[Group]))
FROM FilmAttributeGroup
INNER JOIN deleted ON FilmAttributeGroup.ID = deleted.[Group]
END
--
GO
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question