N
N
nabokovsafran2017-05-30 02:53:46
SQL
nabokovsafran, 2017-05-30 02:53:46

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

2 answer(s)
K
Konstantin Tsvetkov, 2017-05-30
@nabokovsafran

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

Or a connection like:
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

A
Alexander Aksentiev, 2017-05-30
@Sanasol

https://stackoverflow.com/questions/6260688/how-do...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question