I want to create a table with currentdate as DateCreated column in it. So when the data comes in everyday, it should show the current date for that item.. (want to start new Inventory table)
I want to create a table with currentdate as DateCreated column in it. So when the data comes in everyday, it should show the current date for that item.. (want to start new Inventory table)
This is a very simple question, probably a part of homework. A visit to the default definitions section of the create table page would clearly point you in the right direction. Usually, in order to get the answers for homework related questions it is best to show some effort, some work which has been attempted that did not lead to a solution. Nevertheless, this question has been baking here for a long time so here is the answer:
When creating table, define your DateCreated column as date, not datetime since you don't need the time part of the day to be included anyway. Assigning a default value either as current_timestamp or getdate() would do the trick then. Please note that you can just define a default while creating table or optionally define a named constraint. The only difference is that in first case the constraint will be created with some random name. Here is the part of the script, what you are looking for is the line beginning with DateCreated:
set ansi_nulls on; set quoted_identifier on; go create table dbo.Inventory ( -- this is just an example, actual column names need to be used here InventoryID int not null identity(1, 1) primary key clustered, ItemID int not null, -- some other columns' definitions... DateCreated date not null default (current_timestamp), -- etc ); go
You may use getdate() in place of current_timestamp, either one will work. Also, you could explicitly provide constraint name, in which case the line adding DateCreated column should read something like this:
DateCreated date not null constraint DF_Inventory_DateCreated default (current_timestamp),
Hope this helps.
Oleg
18 People are following this question.