fm_strategy
fm_strategy

Reputation: 201

SQL Insert Trigger

I have a table ARCHIVED_TIMESTAMP with columns ID INT, ID_ELEMENT REFERENCES ELEMENT(ID) and ARCHIVED_TIMESTAMP TIMESTAMP

I want to create a trigger that automatically inserts in the table ARCHIVED_TIMESTAMP after every insert in the ELEMENT table the id of the inserted element (ID_ELEMENT=ID) and the timestamp from the insertion(ARCHIVED_TIMESTAMP=CURRENT_TIMESTAMP)

Upvotes: 1

Views: 67

Answers (1)

Roman Marusyk
Roman Marusyk

Reputation: 24609

If I understood correctly then please try something like this:

CREATE  TRIGGER TRG_ELEMENT_FOR_INS ON ELEMENT 
FOR INSERT
AS
BEGIN

  INSERT INTO ARCHIVED_TIMESTAMP(ID_ELEMENT, ARCHIVED_TIMESTAMP)
  SELECT INS.ID
  ,      INS.CURRENT_TIMESTAMP 
  FROM Inserted INS

END -- End trigger TRG_ELEMENT_FOR_INS

Upvotes: 2

Related Questions