简体   繁体   中英

Creation date column in SQL table

What is the easiest way to automatically fill a datetime column in an SQL data table with the date the row was created? Using SQL Server 2005 if that matters.

EDIT: I've tried using GETDATE() for default and that works for an INSERT Query but not when the TableAdapter adds the row.

ALTER TABLE table
ADD column NOT NULL DEFAULT (GETDATE())

You should create the column and have:

  • an insert(after) trigger which sets it to the current date (or date/time).
  • an update(instead-of) trigger which does not allow the column to change.

Any default-value solution is not secure since it can be bypassed by either specifically setting the column when inserting or changing the column afterwards. The trigger-based solution puts the control with the DBMS where it belongs.

use the getdate() function,

so something like: insert data1, data2, getdate() into table1

set a default value for it:

CREATE TABLE dbo.YourTable
    (
    columns....,
    YourDateTime datetime NOT NULL
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.YourTable ADD CONSTRAINT
    DF_YourTable_YourDateTime DEFAULT GETDATE() FOR YourDateTime

when you INSERT don't list that column:

INSERT INTO dbo.YourTable (columns...) VALUES (values...)

or when you INSERT list that column, but use DEFAULT keyword:

INSERT INTO dbo.YourTable (columns...,YourDateTime) VALUES (values...,DEFAULT)

or when you INSERT list that column, and use GETDATE() or some other datetime:

INSERT INTO dbo.YourTable (columns...,YourDateTime) VALUES (values...,'1/1/2010 12:34:56')

As you can see, you have multiple solutions ahead. In short,

  1. You can write your method (from within VBNET) that sets the appropriate field to System.DateTime.Now (not best practice);

     Public Function InsertObject(o As Object) As Integer cmd.CommandText = String.Format("INSERT INTO table_name (Id, CreationDate) VALUES (1, {0})", System.DateTime.Now) Return cmd.ExecuteNonQuery() End Function 
  2. You can write an INSTEAD OF INSERT trigger and replace the the CreationDate inserted value with GETDATE() (recommended way);

     CREATE TRIGGER trg_bi_PersistObject ON objectToPersistTable INSTEAD OF INSERT AS insert into objectToPersistTable (Id, CreationDate) ( select Id, GETDATE() from inserted ) 

    (Please consider that the code is up the top of my head. Some fixes may be needed for syntax purposes or so. This at least gives you the main idea.)

  3. You can set a default value (but it doesn't seem to have worked for you for some reasons).

    (See David M's or KM's answer for code).

As stated in paxdiablo's comment of his own answer, such feature shall be handled on the DBMS side in order to avoid data corruption on the client-side of the application. Then, I bet your better choice might be 2., creating a BEFORE INSERT trigger and setting the value wihtin it with GETDATE() function.

Be careful if you're using replication. I'm not familiar with the internal details of SQL Server replication, but I know that with some forms of MySQL replication, if you use NOW() as the default value or use a trigger using the same function, the dates will differ between the publisher and subscriber databases.

If you set the date in the application, then the date always replicates fine. Of course, the caveat is that the date or time should be set from the application (or web) server, since the date and time on the end user's machine can't be trusted.

If you're using replication, you might want to test this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM