all groups > sql server programming > october 2005 >
You're in the

sql server programming

group:

When does @@DBTS value change?


When does @@DBTS value change? Dean Slindee
10/18/2005 9:50:31 PM
sql server programming:
I have read that the value of @@DBTS changes on every insert and update.
However I am finding that that is not true. I can do multiple inserts of
rows into a table, and all of the rows will have the same value in the
varbinary(8) column that is populated with the value of @@DBTS at the time
of insert.


Single updates to a value in each of these rows also does not change the
varbinary(8) column value either, using the Update stored procedure below.

I have seen the varbinary(8) column value change (on occasion) but I would
expect it to change immediately upon each insert and update. Please correct
my expectation, if wrong.

Thanks,

Dean S

Here is the insert stored procedure:

CREATE Procedure dbo.InsertJusticeClientDrugTimeStamp

@ClientID Int,

@DrugName varchar(15),

@DrugRank tinyint,

@DrugRoute varchar(10),

@DrugAmount varchar(20),

@DrugFrequency varchar(15),

@DrugAgeBegan varchar(5),

@DrugLastUsage VarChar(10),

@DrugLastUsageAmount VarChar(10),

@DrugOtherName varChar(25),

@MaintUser VarChar(10),

@MaintDate SmallDateTime,

@AddUser VarChar(10),

@AddDate SmallDateTime

AS

SET NOCOUNT ON

IF EXISTS (SELECT ClientID FROM tblJusticeClientDrug

WHERE ClientID = @ClientID

AND DrugName = @DrugName)

BEGIN

RETURN -1

END

ELSE

BEGIN

INSERT INTO tblJusticeClientDrug

(ClientID,

DrugName,

DrugRank,

DrugRoute,

DrugAmount,

DrugFrequency,

DrugAgeBegan,

DrugLastUsage,

DrugLastUsageAmount,

DrugOtherName,

MaintUser,

MaintDate,

AddUser,

AddDate,

[TimeStamp])

VALUES (@ClientID,

@DrugName,

@DrugRank,

@DrugRoute,

@DrugAmount,

@DrugFrequency,

@DrugAgeBegan,

@DrugLastUsage,

@DrugLastUsageAmount,

@DrugOtherName,

@MaintUser,

@MaintDate,

@AddUser,

@AddDate,

@@DBTS)

RETURN @@ROWCOUNT

END

GO



Here is the update stored procedure:

CREATE Procedure dbo.UpdateJusticeClientDrugTimeStamp

@ClientID Int,

@DrugName varchar(25),

@DrugRank tinyint,

@DrugRoute varchar(10),

@DrugAmount varchar(20),

@DrugFrequency varchar(15),

@DrugAgeBegan varchar(5),

@DrugLastUsage VarChar(10),

@DrugLastUsageAmount VarChar(10),

@DrugOtherName varChar(25),

@MaintUser VarChar(10),

@MaintDate SmallDateTime,

@AddUser VarChar(10),

@AddDate SmallDateTime,

@TimeStamp varbinary(8)

AS

SET NOCOUNT ON

IF NOT EXISTS (SELECT ClientID FROM tblJusticeClientDrug

WHERE ClientID = @ClientID

AND DrugName = @DrugName

AND [TimeStamp] = @TimeStamp)

BEGIN

RETURN -1

END

ELSE

BEGIN

UPDATE tblJusticeClientDrug

SET DrugRank = @DrugRank,

DrugRoute = @DrugRoute,

DrugAmount = @DrugAmount,

DrugFrequency = @DrugFrequency,

DrugAgeBegan = @DrugAgeBegan,

DrugLastUsage = @DrugLastUsage,

DrugLastUsageAmount =
@DrugLastUsageAmount,

DrugOtherName = @DrugOtherName,

MaintUser = @MaintUser,

MaintDate = @MaintDate,

AddUser = @AddUser,

AddDate = @AddDate,

[TimeStamp] = @@DBTS

WHERE ClientID = @ClientID

AND DrugName = @DrugName

RETURN @@ROWCOUNT

END

GO

Re: When does @@DBTS value change? Louis Davidson
10/18/2005 10:38:23 PM
The @@dbts value is pretty much useless,though it is interesting. It shows
what the next rowversion (timestamp) will be whenever you do an insert or
update to any table in the database with a rowversion (timestamp) column.

For example:

set nocount on
drop table testRowversion
go
create table testRowversion
(
value varchar(10),
version rowversion
)
go
select @@dbts as lastRowversion
go
insert into testRowversion (value)
values ('one')
go
select * from testRowversion
select @@dbts as lastRowversion
go
update testRowversion
set value = 'changed'
go
select * from testRowversion
select @@dbts as lastRowversion
go

Returns:

lastRowversion
------------------
0x0000000000000066

value version
---------- ------------------
one 0x0000000000000067

lastRowversion
------------------
0x0000000000000067

value version
---------- ------------------
changed 0x0000000000000068

lastRowversion
------------------
0x0000000000000068

--
----------------------------------------------------------------------------
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)

[quoted text, click to view]

Re: When does @@DBTS value change? Brian Selzer
10/18/2005 11:42:36 PM
The global variable @@DBTS changes whenever a row is actually inserted into
a table in the current database that has a rowversion column. If the INSERT
statement has @@DBTS in the SELECT clause, then @@DBTS is only read once
prior to the actual insert.
Don't depend on @@DBTS. In a concurrent environment, it has even more
volatility than @@IDENTITY, and there isn't any SCOPE_DBTS() function in
Transact-SQL that works like SCOPE_IDENTITY().

[quoted text, click to view]

RE: When does @@DBTS value change? ML
10/19/2005 1:31:03 AM
Why are you doing this? Look up ROWVERSION in Books Online. The rowversion
column is maintained by the system, you do not need (better: cannot) affect
its value - it's set by the system at insert and on each update.


AddThis Social Bookmark Button