2015-07-28 218 views
1

我有一個表ARCHIVED_TIMESTAMP的列ID INTID_ELEMENT REFERENCES ELEMENT(ID)ARCHIVED_TIMESTAMP TIMESTAMPSQL插入觸發器

我想創建一個觸發器,在ELEMENT表中插入的元素的id每次插入後自動插入表中的ARCHIVED_TIMESTAMPID_ELEMENT=ID ),並從插入時間戳(ARCHIVED_TIMESTAMP=CURRENT_TIMESTAMP

+2

到目前爲止你做了什麼? – mic4ael

+0

也許最好使用CDC爲此 - https://www.simple-talk.com/sql/learn-sql-server/introduction-to-change-data-capture-(cdc)-in-sql-server- 2008 / –

回答

2

如果我理解正確,那麼請嘗試是這樣的:

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