问题
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