TimeStamp datatype in SQL SERVER 2000

Well, believe me or not but right now I am in the back seat of the car and going to San Antonio. Anyway, good thing I charged my laptop so I can play around with SQL SERVER 2000.

When working with timestamp datatype and you want to get the last timestamp value inserted you can simply use the @@DBTS.

CREATE TABLE #testts ( k1 int identity(1,1) PRIMARY KEY, updatelog timestamp) 

INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 

SELECT @@DBTS As 'Last timestamp inserted'

In the above code I am creating a #testts table which is a temp table. If I am not in the tempdb (where the temp tables are stored) then the timestamp value returned will not be the correct one. In order to receive the correct timestamp value you have to be in the database where you inserted the row.

You can do something like this:

USE tempdb
CREATE TABLE 
#testts ( k1 int identity(1,1) PRIMARY KEY, updatelog timestamp) 

INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 
INSERT INTO #testts DEFAULT VALUES 

SELECT @@DBTS As 'Last timestamp inserted' 

The above code will return the correct timestamp value.



 

powered by IMHO 1.3

Print | posted @ Sunday, December 25, 2005 3:13 PM

Twitter