简体   繁体   English

每当SQL Server中的该行发生更新时,“自动增量列”值

[英]Auto Increment Column value whenever update happen on that row in sql server

I want to maintain a column which will store that how many times a row has been modified. 我想维护一列,该列将存储一行已被修改的次数。 So whenever the row has been updated I want to increase the column value. 因此,无论何时更新行,我都想增加列值。 I think I have to use trigger for that.But I am looking for an alternative solution. 我认为我必须为此使用扳机,但我正在寻找替代解决方案。

IMHO trigger is the way to go, but if you sure that you control all your updates, then you can do as simple as this: 恕我直言,触发是一种方法,但是,如果您确定可以控制所有更新,则可以像下面这样简单:

UPDATE mytable 
   SET somefield='newvalue', 
       update_count = update_count+1 
   WHERE id=n
CREATE TRIGGER CountRows 
    ON TestCount 
    after Update
AS 
Update TestCount  set Cnt = Cnt +1 where ID in (select ID from inserted)
GO

whenever some value in a row changes, the grigger adds +1 to the same row's Cnt column value. 每当一行中的某些值更改时,触发器将+1添加到同一行的Cnt列值。

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

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