Understanding TIMESTAMP (ROWVERSION) in SQL Server
page 5 of 7
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 47321/ 382

Benefit of TIMESTAMP in multi-user access on TABLE

Hence, TIMESTAMP/ROWVERSION provides a row-versioning feature in the table. Row-versioning can be used to examine the changes in table. It can also help to manage the synchronization in multiuse access. For example, if two users, A and B, are accessing a table T at the same time. Both are intended to make some changes in a set of rows in T by using the latest values available in that. Both set A and B can read the required values from rows with the TIIMESTAMP column values. Before going to update the value in specific row in the table, if both A and B checks if the already read TIMESTAMP value still matches the TIMESTAMP value of the same row in the table, then they can go for update. If the TIMESTAMP value differs then it means another user has already modified that row, so it requires reading that table (at least modified rows) again to get the updated values. In this case, both user A and B are synchronized to each other.

Few more operations with TIMESTAMP

In general we do not need to update a timestamp column value manually, but if we need to do it then we can use binary and varbinary values to update a timestamp column value.

Though you can use integer values to assign in timestamp type variable in SQL. For example:

Listing 9

DECLARE @timest AS TIMESTAMP
SET @timest = 5
SELECT @timest
If you execute the above query this will result following output:
 0x0000000000000005

Now if you try following a query to insert a row with specific timestamp value in the above created table:

Listing 10

INSERT INTO MyTestTable([TIMESTAMP],[SmallDateTime],[DateTime])
VALUES(5,GETDATE(), GETDATE())

it will show the following error message:

Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column.

As the error message indicates, we can either remove the timestamp column from the insert statement or set DEFAULT keyword as the value to automatic timestamp value to be inserted.

Listing 11

INSERT INTO MyTestTable1([TIMESTAMP],[SmallDateTime],[DateTime])
VALUES(DEFAULT,GETDATE(), GETDATE())

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 4 and type the answer here:

User Comments

Title: Understanding TIMESTAMP (ROWVERSION) in SQL Server   
Name: Stuart Steedman
Date: 1/26/2011 4:08:12 AM
Comment:
Thanks, Abhisheck. Good article. I can now see how the timestamp column could be used to reliably filter all new/changed records.

Stu
Title: Exact use of timestamp   
Name: Anu
Date: 10/8/2010 7:01:33 AM
Comment:
please tell me when and why tp use timestamp.. Confused a lot..
Title: Mr   
Name: tv Krishna Kanth
Date: 7/20/2010 7:33:53 AM
Comment:
To know the latest Timestamp in table you can write a query like "select @@dbts from tblName" .. Yes TimeStamp is datatype which is ofcourse not related to any date & time like data types. here dbtp means data base time stamp you can remember simply with this expansion.. When defining/using a timestamp in a table no need to declare its column name..But the rowversion would ask for the column name eventhough they both are similar in functionality as per my knowledge..
Title: Nonsense   
Name: RIG
Date: 4/23/2010 6:43:30 AM
Comment:
"Wikipedia.org defines timestamp in very clear manner, as follows:

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred."

Right. But TIMESTAMP in MS SQL Server has nothing to do with date or time.
Title: excellent   
Name: Jeetendra
Date: 4/16/2010 3:52:33 AM
Comment:
This is Excellent. Thanks.
Title: good   
Name: saravanan
Date: 1/16/2010 2:20:08 AM
Comment:
good article
Title: timestamp problem   
Name: arpizt
Date: 10/29/2009 9:35:55 PM
Comment:
"UPDATE table SET field = current_timestamp where (field=1)"
Error msg this field cannot been update.

Thanks.
Title: arpizt@gmail.com   
Name: abhishek
Date: 10/28/2009 6:41:21 PM
Comment:
arpizt@gmail.com can you post your query here?
Title: timestamp problem   
Name: arpizt@gmail.com
Date: 10/27/2009 5:53:58 AM
Comment:
Hi,

I try to run the insert sql, but got this error ("Column or expression 'PaymentDate' cannot be updated." Why? How to overcome this problem?
Title: Abhishek Your Time Stamp Aarticle too good   
Name: Gbbs Mith
Date: 6/13/2009 5:02:18 AM
Comment:
The article is very good because it was justified clearly time and datetime data type. Hope more such article

Thanks
Title: Beginner   
Name: nagarajan
Date: 3/17/2009 9:47:06 AM
Comment:
excellent artice about TIMESTAMP.
Title: good   
Name: Jen
Date: 12/18/2008 4:57:37 AM
Comment:
good for quick walkthrough.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2012 ASPAlliance.com  |  Page Processed at 2/12/2012 12:17:27 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search