Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : on update set current timestamp

Tags:

sql-server

I need a timestamp field which updates every time the user modifies the record.

So far I used MySql in which I can even use this in the field creation:

ALTER TABLE myTable 
    ADD `last_time` timestamp NOT NULL 
        DEFAULT CURRENT_TIMESTAMP 
        ON UPDATE CURRENT_TIMESTAMP

I couldn't find this possibility in SQL Server.

Then I tried writing a trigger - in a MySql trigger this is simple:

SET new.last_time = CURRENT_TIMESTAMP();

SQL Server doesn't seem to know neither new, nor old syntax, it gave me error on compilation.

This:

UPDATE myTable 
SET last_time = CURRENT_TIMESTAMP;

worked, but it updated all the rows instead of the current.

Isn't there a way the tell SQL Server to update the current record? Should I use UPDATE .... WHERE myid = something ?

Doesn't SQL Server know which is the actual record it is processing?

like image 394
starhu Avatar asked Mar 23 '14 17:03

starhu


People also ask

How do you update a timestamp in SQL?

Syntax – Update value to Current Timestamp ALTER TABLE table_name updates table schema. CHANGE column_name updates the column to. column_name TIMESTAMP NOT NULL defines the column as of datatype TIMESTAMP. DEFAULT CURRENT_TIMESTAMP sets the default value of the column to CURRENT_TIMESTAMP.

How can update only date in datetime column of table in SQL Server?

Just use varchar and modify what you want in it without touch the time. In this example I use CONVERT(varchar(12), columnDatetime) to get a string with length of 12 characteres assuming a case of time with a format for example like "20:10:15.250".

How do I get the current timestamp in SQL Server?

The CURRENT_TIMESTAMP function returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the GETDATE() function.

What is current timestamp in SQL?

The CURRENT TIMESTAMP (or CURRENT_TIMESTAMP) special register specifies a timestamp that is based on a reading of the time-of-day clock when the SQL statement is executed at the application server.


1 Answers

And if you really need a timestamp - then make a trigger on insert and update that updates the column with the current timestmap.

CREATE TRIGGER dbo.trgAfterUpdate ON dbo.YourTable
AFTER INSERT, UPDATE 
AS
  UPDATE dbo.YourTable
  SET last_changed = GETDATE()
  FROM Inserted i

To update a single row (which has been edited or inserted) you should use

CREATE TRIGGER dbo.trgAfterUpdate ON dbo.YourTable
AFTER INSERT, UPDATE 
AS
  UPDATE f set LastUpdate=GETDATE() 
  FROM 
  dbo.[YourTable] AS f 
  INNER JOIN inserted 
  AS i 
  ON f.rowID = i.rowID;

These should be all you need. GETUTCDATE() if you want it in UTC (which I prefer)

SQL Server absolutely knows the rows it processes

update myTable set last_time =CURRENT_TIMESTAMP ; worked, but it updated all the rows instead of the current.

Yeah, guess what - because that is exactly what you tell SQL Server: Update all rows in the table.

Doesn't Sql Server know which is the actual record it is processing?

Sets have no current row ;) That is where the problem starts.

The only way to do that exactly as you want is up in my answer on the beginning: a timestamp. Due to the misconceptions, though, I add an advice: get a book about SQL basics.

like image 71
TomTom Avatar answered Oct 02 '22 03:10

TomTom