简体   繁体   中英

CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in mssql?

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) 
    )

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

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