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 开发者_如何学JAVAtried 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.
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')
use the getdate()
function,
so something like: insert data1, data2, getdate() into table1
As you can see, you have multiple solutions ahead. In short,
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
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.)
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.
精彩评论