created and modified timestamp in sql server 2008 -
i new sql , want create timestamp createdtime , modifiedtime columns. , compare createdtime modifiedtime, if modifiedtime greater 10 minutes not let row updated. how can achieve this? using sql server 2008 r2 thanks
created timestamp easy: column of type datetime
(for sql server 2005) or datetime2(3)
(2008 or newer) default constraint sysdatetime()
sql server 2000/2005:
create table dbo.yourtable ( ..... columns ......, createdtimestamp datetime not null constraint df_yourtable_created default (getdate()) )
sql server 2008 , newer:
create table dbo.yourtable ( ..... columns ......, createdtimestamp datetime2(3) not null constraint df_yourtable_created default (sysdatetime()) )
the modified timestamp must handled trigger on each table
create trigger trgyourtablemodified after update on dbo.yourtable update dbo.yourtable set modifieddate = getdate() -- or use sysdatetime() 2008 , newer inserted i.id = dbo.yourtable.id
in trigger, check yoru 10 minute gap - , if difference more 10 minutes, call rollback transaction
in trigger stop update
you need aware triggers in sql server per statement - , not per row! if update
statement updates 50 rows, trigger fire once , inserted
, deleted
pseudo-tables inside trigger contain 50 rows deal with.
Comments
Post a Comment