CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in mssql?

做~自己de王妃 提交于 2021-01-08 02:45:26

问题


Here i am trying to use CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in mssql. Here getdate() is available.How can i rewrite with current-date with updated date automatically in sql.

Code I am trying to update:

  CREATE TABLE XXXXX
    ( fid int(11) NOT NULL AUTO_INCREMENT,
     Name varchar(100) NOT NULL, 
    status varchar(4) DEFAULT 'yes',
     fdate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP  
 ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (fid) 
    )

回答1:


Create database

 CREATE TABLE XXXXX
( 
    fid int NOT NULL IDENTITY PRIMARY KEY,
    Name varchar(100) NOT NULL, 
    status varchar(4) DEFAULT 'yes',
    fdate datetime NOT NULL DEFAULT getdate() 
)

Create trigger

CREATE TRIGGER Update_fdate
   ON  XXXXX
   AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;
    UPDATE XXXXX
    SET [fdate] = GETDATE()
    WHERE fid IN (SELECT fid FROM Inserted)
END

Now it will be updated if someone update it...

If you want to be null just change to NULL and remove the DEFAULT value



来源:https://stackoverflow.com/questions/35479335/current-timestamp-on-update-current-timestamp-in-mssql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!