dbTalk Databases Forums  

Update Guid column on any update to table row

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Update Guid column on any update to table row in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
michael
 
Posts: n/a

Default Update Guid column on any update to table row - 07-23-2007 , 08:27 AM






I have a column Last_Updated (uniqueidentifier) on a table. I'd like
this column to get a new guid (NEWID()) each time any column in the
row is updated.

Is there an automatic way to do this outside of triggers?

Here's an example of what I'm looking for:

CREATE TABLE GuidTest (ID INT PRIMARY KEY IDENTITY, Status INT,
LastUpdated UNIQUEIDENTIFIER DEFAULT (NEWID()))
INSERT INTO GuidTest (Status) VALUES (10)
INSERT INTO GuidTest (Status) VALUES (20)
INSERT INTO GuidTest (Status) VALUES (30)

SELECT ID, Status, LastUpdated FROM GuidTest
UPDATE GuidTest SET Status = 31 WHERE ID = 3
SELECT ID, Status, LastUpdated FROM GuidTest
-- Would like GUID on ID=3 to be different in second select

DROP TABLE GuidTest

Thanks,
Michael


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: Update Guid column on any update to table row - 07-23-2007 , 08:41 AM






Outside of a trigger, no. But it is exactly the sort of thing that
triggers are for.

Roy Harvey
Beacon Falls, CT

On Mon, 23 Jul 2007 13:27:13 -0000, michael <mharen (AT) gmail (DOT) com> wrote:

Quote:
I have a column Last_Updated (uniqueidentifier) on a table. I'd like
this column to get a new guid (NEWID()) each time any column in the
row is updated.

Is there an automatic way to do this outside of triggers?

Here's an example of what I'm looking for:

CREATE TABLE GuidTest (ID INT PRIMARY KEY IDENTITY, Status INT,
LastUpdated UNIQUEIDENTIFIER DEFAULT (NEWID()))
INSERT INTO GuidTest (Status) VALUES (10)
INSERT INTO GuidTest (Status) VALUES (20)
INSERT INTO GuidTest (Status) VALUES (30)

SELECT ID, Status, LastUpdated FROM GuidTest
UPDATE GuidTest SET Status = 31 WHERE ID = 3
SELECT ID, Status, LastUpdated FROM GuidTest
-- Would like GUID on ID=3 to be different in second select

DROP TABLE GuidTest

Thanks,
Michael

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Update Guid column on any update to table row - 07-23-2007 , 05:08 PM



michael (mharen (AT) gmail (DOT) com) writes:
Quote:
I have a column Last_Updated (uniqueidentifier) on a table. I'd like
this column to get a new guid (NEWID()) each time any column in the
row is updated.

Is there an automatic way to do this outside of triggers?
I don't know what the purpose with this guid is, but there is a special
data type in SQL Server, timestamp, for this purpose. A timestamp is a
binary(8) values with no relation to date and time. Such a column is
automatically updated each time a row is touched. Furthermore, the
value is database-unique and monotonically growing.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.