views:

1888

answers:

4

I am writing a number of scripts to update numerous Access tables. I would like to add a column to each that has a field called "date_created" that is a timestamp of when the record is created. Doing this through the table view is simple, simply set the DefaultValue = now(). However, how would I accomplish this in sql?

This is my current attempt for tables that already have the column. This example uses "tblLogs".

ALTER TABLE tblLogs ALTER COLUMN date_created DEFAULT now()

Thanks for your help!

Update - Would there be a way to do this in VBA?

Update 2 - Tested all of the answers and the following by onedaywhen is the shortest and most accurate

CurrentProject.Connection.Execute _
    "ALTER TABLE tblLogs ALTER date_created DATETIME DEFAULT NOW() NOT NULL;"
+2  A: 

Instead of now() you can use getdate(). Can't check other part of syntax (no sql here and I rarely change tables :)), but should be about same.

edit: Seems that SQL doesn't allow change defaults so easily. Look at this page, where practical example is provided - you need to change constraints, because MSSQL treats defaults as constraints. Sorry for misinformation.

Arvo
I tried that and it did not work. Thanks for answering though.GetDate() will bring back today and but not the exact second and will be nearly the same as Now() as far as I know.
Kevin Lamb
getdate() returns full time, don't worry.
Arvo
Apparently MSSQL is a bit harder with defaults - you have to drop/create specific constraints. I edited my answer to reflect this.
Arvo
Wow thats really odd. Nice find!
Kevin Lamb
I would have thought that "Microsoft Access DateTime Default Now via SQL" meant they were using ACE/Jet rather than SQL Server.
onedaywhen
Hmm, seems that I misread original question (my english is not good) and/or based my answer on misinterpreting tags (sql) - but why is my MSSQL answer accepted then?
Arvo
A: 

In SQL Server it would be:

ALTER TABLE tblLogs ALTER COLUMN date_created DEFAULT getDate()

James Conigliaro
A: 

I suggest that to create or restructure Jet tables, you use DAO instead of DDL. It offers the ability to control properties that are lacking in Jet's implementation of DDL. The Access help should provide you what you need on this.

David-W-Fenton
Those "properties that are lacking in Jet's implementation of DDL" are rather obscure (do *you* ever use them?!) and definitely do not include DEFAULT! But the OP has also asked for some VBA so how about you post your many lines of equivalent DAO code to see if they prefer it to either the single line of SQL DDL or single line of Access+ADO code? :)
onedaywhen
I don't create tables in code of any form. I don't see the point.
David-W-Fenton
Regardless of how you create tables, do you ever explicitly use those "properties that are lacking in Jet's implementation of DDL"?
onedaywhen
BTW the point of creating tables in code, rather than a GUI, is so your actions can be repeated and reproduced e.g. if I want to describe a ACE/Jet feature here on SO I tend to post a VBA script to fabricate a .mdb in Windows temp folder, create tables, insert test data and execute SQL tp reproduce the problem. IMO that is making things as easy as possible for the reader to reproduce the scenario. If I instead posted descriptions of how to manipulate the Access UI to do the same I doubt as many folk would be bothered to actually reproduce the scenario.
onedaywhen
I've been posting in online Access forums since 1996 and have never once needed to post DDL to describe a table structure, so I consider your point extremely weak. Most of the times I see DDL describing the whole table structures, I see TOO MUCH INFORMATION, and not enough pruning of the post down to the essential point. For those who are not very good at asking questions in a technical forum, perhaps more info. is better, but I'd rather see people do a better job of wording their question, rather than providing tons of information that doesn't apply to the specific question.
David-W-Fenton
I now agree your original comment: you really don't see the point, do you! :)
onedaywhen
+1  A: 

I'm assuming:

  • your target database is the ACE or Jet engine, rather than SQL Server;
  • you want the date and time i.e. what is known as CURRENT_TIMESTAMP in Standard SQL (not directly supported by ACE/Jet, though) and not to be confused with SQL Server's TIMESTAMP data type;
  • you want an answer using SQL DDL.

If you created your table using this SQL DDL:

CREATE TABLE tblLogs 
(
   date_created DATETIME NOT NULL
);

then the following SQL DDL would add the DEFAULT your require:

ALTER TABLE tblLogs ALTER 
   date_created DATETIME DEFAULT NOW() NOT NULL;

The above ACE/Jet SQL syntax is ANSI-92 Query Mode flavour. To use this via the MS Access interface (e.g. a Query object's SQL view) see Microsoft Office Access: About ANSI SQL query mode (MDB). To do this programmatically using SQL DDL requires (AFAIK) the use of OLE DB, which in VBA requires (AFAIK) the use of ADO. DAO always uses ANSI-89 Query Mode syntax, whose SQL DDL syntax lacks support for DEFAULT.

If you are using Access (rather than ACE/Jet standalone), you can use a single line of VBA (ADO) code:

CurrentProject.Connection.Execute "ALTER TABLE tblLogs ALTER date_created DATETIME DEFAULT NOW() NOT NULL;"
onedaywhen