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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With