Interview Question in SQL Server Clustering


 

Interview Question :: Creating a last modified date in SQL Server 2005


I'm new to SQL Server after using Access for many years and need some help setting up a time stamp to show the last time a record was updated. Here is part of my table script and the column that needs to be updated is "time_stamp". Any help is appreciated and please remember I'm new to SQL Server. The simpler the solution the better. Thanks.

CREATE TABLE [dbo].[genericAccountDB](
[accountId] [varchar](50) NOT NULL,
[accountServerID] [varchar](50) NOT NULL,
[accountPlatform] [varchar](25) NOT NULL,
[accountUsers] [text] NOT NULL,
[time_stamp] [smalldatetime] NOT NULL,
CONSTRAINT [PK_genericAccountDB] PRIMARY KEY CLUSTERED
(
[accountId] ASC,
[accountServerID] ASC
Answers to "Creating a last modified date in SQL Server 2005"
RE: Creating a last modified date in SQL Server 2005?

What you want is a "trigger" on that table.



I use them quite a bit to update date/time and user id fields.



Create Trigger tiu_genericAccountDB

on genericAccountDB with encryption

for Insert, Update

as

UPDATE genericAccountDB Set time_stamp = GetDate()

FROM inserted i, genericAccountDB

WHERE i.AccountID= a.AccountID

end

go



I might be missing a few details...doing this from memory. Hope it helps.
 
Vote for this answer ::  
RE: Creating a last modified date in SQL Server 2005?

To answer the question more directly, you want to use Transact SQL's 'getdate()' function. See the UPDATE statement in guitarslinger_67's comment to see how to use it.
 
Vote for this answer ::  
Update Alert Setting