Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update sql server timestamp column without changing record data

I have an sql server table with a timestamp column. Is there a way to force the timestamp column to change without an actual update of the record?

The reason I am asking is because I want the record's timestamp to change when a record is inserted/updated/deleted in a child table.

Timestamp may not be the best way to go about doing this. If not, what should I do? I don't want to use datetime because I don't feel that's a good way of versioning. I don't want to use an integer, because I don't want to have to read the value to increment it.

Suggestions?

like image 651
Ronnie Overby Avatar asked Jan 29 '10 21:01

Ronnie Overby


People also ask

How do I change a TIMESTAMP column in SQL Server?

You unfortunately cannot make a change to a timestamp column, as the error implies; you are stuck with what you have. Also, each table can only have one timestamp column, so you cannot duplicate the column in any solution.

How can I UPDATE a datetime field in SQL?

To update with the current date and time: UPDATE table_name SET date_field = CURRENT_TIMESTAMP; To update with a specific date value: UPDATE table_name SET date_field = 'YYYY-MM-DD HH:MM:SS.

How do I change a TIMESTAMP in SQL query?

Syntax – Update value to Current TimestampALTER 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 do you UPDATE a specific record in an existing table?

The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected.


1 Answers

I don't want to use an integer, because I don't want to have to read the value to increment it.

UPDATE Table SET
    IntColumn = IntColumn + 1

While that does technically require a read, I don't see any problems with it.

You could always just update to the same value:

UPDATE Table SET
    SomeColumn = SomeColumn

which will trigger rowversion update as well.

ADDITION: You could do a view with the max rowversion of the children:

SELECT Parent.*, MaxChildRowVersion as ChildVersion
FROM Parent
JOIN (
    SELECT ParentId, MAX(RowVersion) as MaxChildRowVersion
    FROM Child
    GROUP BY ParentId
) as Child ON
     Parent.ParentId = Child.ParentId

But, no, you can't directly update a rowversion column (though you could implement your own updatable with @@DBTS, binary(8) and INSTEAD OF triggers...)

Could you give examples of your last point? It sounds promising.

No, really it doesn't. ;) It's too much work when you could just update to the same value instead or use a view. Those are the 2 easiest options.

But, to be complete, a binary(8) column with a default of @@DBTS (which returns the database version number), and an AFTER UPDATE trigger that also updates the binary column to the new @@DBTS will give you a psuedo-rowversion type that you can then update manually. It'd be no faster or better than just updating some other column to it's same value though.

like image 157
Mark Brackett Avatar answered Oct 16 '22 17:10

Mark Brackett