简体   繁体   English

SQL Server 2008 R2触发器

[英]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. 我知道有很多Q问这个问题,但在看完之后,似乎没有人像我一样遇到同样的麻烦。

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. 我现在正在研究创建一个独立的表,当记录更新时,该表通过触发器使用gkey和当前日期时间戳进行更新。

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: 这给了我一个类似下面的输出,但有917行相同:

+---------+-------------------------+
| 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: 您只需要访问inserteddeleted伪表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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