简体   繁体   中英

SQL Server 2008 R2 Trigger

I know there have been a plethora of Q's asked around this but after reading, nobody seems to have the same trouble as I do.

Background Info: I am looking to doing incremental updates into a Cube, but have no timestamp of update on the table. I now am looking at creating a standalone table which updates via a trigger with the gkey and current datetime stamp, when a record is updated.

My trigger on the table is as follows:

CREATE TRIGGER trgAfterUpdateVisitDetails ON  [visit_details]
FOR UPDATE
AS  
begin
   declare @VD_Gkey nvarchar
   declare @today DATETIME = SYSDATETIME()

   insert into [UpdatedVD]
     SELECT 
        @VD_Gkey, @today
     FROM 
        [visit_details]
End

This gives me an output like below but with 917 lines of the same:

+---------+-------------------------+
| VD_Gkey |          today          |
+---------+-------------------------+
| NULL    | 2014-01-02 11:21:23.963 |
| NULL    | 2014-01-02 11:21:23.963 |
+---------+-------------------------+

I know it's going to be something pretty simple but I cannot get my head around this.

Any help would be brilliant.

Thanks.

You just need to access the inserted and deleted pseudo-tables:

CREATE TRIGGER trgAfterUpdateVisitDetails ON  [visit_details]
FOR UPDATE
AS  
begin
    insert into 
        [UpdatedVD]
    SELECT 
        i.VD_Gkey, CURRENT_TIMESTAMP
    FROM
        inserted i;
End

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM