Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2008 Row Insert and Update timestamps
    primarykey
    data
    text
    <p>I need to add two columns to a database table in SQL Server 2008 R2:</p> <ul> <li>createTS - <em>date and time when row is inserted</em></li> <li>updateTS - <em>date and time when row is updated</em></li> </ul> <p>I have a few questions:</p> <ol> <li>What column data type should I employ for each of these?</li> <li><code>createTS</code> needs to be set only once, when the row is inserted. When I tried the <code>datetime</code> type for this column and added a <em>Default Value or Binding</em> of <code>getdate()</code>, the column value was appropriately set. Is this the best way to fulfill the purpose of this column? I considered <code>timestamp</code> data type, but that is, in my opinion, nearly a misnomer!</li> <li><code>updateTS</code> needs to be set to the date and time of the moment when the row is updated. In SQL Server, there is no <em>ON UPDATE CURRENT_TIMESTAMP</em> (as in MySQL), so it looks like I have to resort to using a trigger. Is this right and how would I go about doing that?</li> </ol> <p>So there is a starting point for anyone who would like to answer this question, here is the create table script:</p> <pre><code>CREATE TABLE [dbo].[names] ( [name] [nvarchar](64) NOT NULL, [createTS] [datetime] NOT NULL CONSTRAINT [DF_names_createTS] DEFAULT (getdate()), [updateTS] [datetime] NOT NULL, CONSTRAINT [PK_names] PRIMARY KEY CLUSTERED ( [name] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload