Skip to main content

About TimeStamp datatype of SQL Server ...

SQL Server has a less known datatype called “TimeStamp”. But I wasn't able to find any article about it on the web. So I thought I would try a sample myself and write about it.

TimeStamp is actually used for record versioning in a table. When you Insert (or) Update a record in a table with a TimeStamp field, that field gets updated automatically.

Lets create a sample table to understand the usage of TimeStamp field.

Create table TimeStampExample
(
RunningNumber int identity,
LastName varchar(30),
tStamp timestamp
)

The above table “TimeStampExample” has been created with a TimeStamp field (tStamp). By the way its not mandatory to provide a field name for timestamp columns. The below table structure is perfectly valid only.

Create table TimeStampExample
(
RunningNumber int identity,
LastName varchar(30),
timestamp -- note we haven't mentioned a field name here
)

When a record is inserted the value of tStamp gets automatically set by SQL Server. Lets see that in action.

Insert TimeStampExample (LastName) values ('RajiniKanth')

After inserting a sample record lets query the table to see the complete record with timestamp value in it.

Select * from TimeStampExample

We need to understand that TimeStamp are just binary values that are incremented with every change in the database and therefore they are unique with the database.

For better understanding lets update the record and check whether tStamp has been updated or not. Make use of the below code snippet for checking that.

Update TimeStampExample Set LastName = 'Shivaji Rao' Where LastName = 'RajiniKanth'

Just query the table now and you could see that the TimeStamp field being updated automatically. Cool isn't it :)

Comments

byerspc said…
You have a couple typos

values ( needs single quote on both sides)

you meant note when you wrote not

Thanks for the info
Vadivel said…
Byerspec, Thanks for pointing out the Typo, I have corrected it now.
Anonymous said…
Nice try mate,

But things arent that simple and cool with this data type. Microsoft itself suggests not to use this Data Type anymore as there are several problems with it, for example:

1. You cant query on what date and time a particular row was updated (with the help of timestamp I mean)

2. In order to query on Timestamp, you got to know and store the DBTS Values somewhere so that you can use those Binary Values in your query. This can be done by querying SELECT @@DBTS, GETDATE()

3. The other major problem with Timestamp is that it generates a guaranteed Unique Value in one Database on one Server. If you restore the Database on a different Server, the DBTS Values are reset and that might upset your whole system.

4. In order to prevent the above from happening, you got to Restore the MODEL database first on a new server and then your database. But in some cases that too doesnt work properly.

5. Also note that the only reason Microsoft still keeps this Datatype is the backward compatibility. This type came to SQL Server when they bought it from Sybase 4.2. They had to keep this Data Type as they wanted to support the existing people who are already using it in their systems.

5. Currently, SQL Server only uses this data type for Recovery operations.

Have you ever noticed all these problems ?

Nariman Ghanghro
George Jetson said…
Where's the love for my boy the timestamp column? ;)

I utilize the timestamp column in a third-party vendor source system to identify the delta (what has changed since my last extraction.) And while this isn't the first club I'd reach for usually, given that the vendor includes and exposes the timestamp column in most major tables (and marks rows as retired in lieu of row deletions) but doesn't provide a vendor-blessed API or way to identify the delta, I've found it useful.

1. You cant query on what date and time a particular row was updated (with the help of timestamp I mean)

Once I've identified the delta in the source system, I utilize my own controlled (and frozen) SequenceId
in the downstream warehouse.


2. In order to query on Timestamp, you got to know and store the DBTS Values somewhere so that you can use those Binary Values in your query. This can be done by querying SELECT @@DBTS, GETDATE()

Absolutely. The timestamp value varies in the source system. I convert it's value to a varbinary(8) to freeze it in the warehouse. With the knowledge of the the frozen varbinary(8)'s in the warehouse, I can ask "What's new or modified?" in the source system.

3. The other major problem with Timestamp is that it generates a guaranteed Unique Value in one Database on one Server. If you restore the Database on a different Server, the DBTS Values are reset and that might upset your whole system.

So there may be a discontinuity in the robustly monotonically increasing timestamp values upon a restoration event. So, as mentioned above, I work in my own frozen varbinary(8)'s, and I provide a varbinary(8) source adjustment in case a server switch makes it necessary.

I can't wait for SQL 2008's data change capture capability to make all of this unnecessary.
Gabbar said…
Nice info Vadivel and George.

Popular posts from this blog

Script table as - ALTER TO is greyed out - SQL SERVER

One of my office colleague recently asked me why we are not able to generate ALTER Table script from SSMS. If we right click on the table and choose "Script Table As"  ALTER To option would be disabled or Greyed out. Is it a bug? No it isn't a bug. ALTER To is there to be used for generating modified script of Stored Procedure, Functions, Views, Triggers etc., and NOT for Tables. For generating ALTER Table script there is an work around. Right click on the table, choose "Modify" and enter into the design mode. Make what ever changes you want to make and WITHOUT saving it right click anywhere on the top half of the window (above Column properties) and choose "Generate Change Script". Please be advised that SQL Server would drop actually create a new table with modifications, move the data from the old table into it and then drop the old table. Sounds simple but assume you have a very large table for which you want to do this! Then it woul

AWS fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request

While using AWS and trying to copy a file from a S3 bucket to my EC2 instance ended up with this error message. Command Used: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ Error: fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request The error goes off if we add the region information to the command statement. I am using Asia Pacific (Mumbai) so used ap-south-1 as the region name. Modified Command: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ --region ap-south-1

[Non Tech] Want to know the recipe for Omelette :)

Fed up with Bread - Jam and Curd Rice, today i wanted to eat Omelette. Interesting part is I wanted to cook it myself :) So in the first picture you see all the items which are needed for preparing an Omelette. When I had a closer look at the eggs I see that almost all the eggs are broken. But believe me when I bought it couple of days back it was in perfect condition! I was wondering whether the eggs have become rotten or pretty old to consume! I tried taking an egg and break it but couldn't break it at all :) Since I have kept in the freezer all the eggs have frozen and looked like a iron ball :) After trying for few minutes of trying i removed the shell of the egg and then kept that iron ball :) into a bowl and placed it within Oven. I heated it for 1 minute and checked. It melted only to a limit. So i just set it for another 2 minutes and checked it later. It has melted but the part of the egg white has become a Omelette :( I didn't leave it there. I took the bowl out of