Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
3/17/2009 12:19:32 PM EDT
My wife is taking a SQL class and she's having a problem with one of her projects, I know absolutely nothing about SQL as i'm more of a hardware/software/network person so I figured i'd ask here.  The question states :

Using the alter table command, add the current system date and time as the default value to the order date column of the orders table.  
The professor has instructed her to use this:

ALTER TABLE Orders              DATE   DEFAULT SYSDATE

She's getting a syntax error on default and sysdate.

She tried this  and got it to change the column properties to date but not sysdate

ALTER TABLE Orders
ALTER COLUMN OrderDate  DATE
GO

Anyone have any ideas?
3/17/2009 3:41:03 PM EDT
[#1]
This is for SQL2005.


–– create the table
CREATE TABLE [dbo].[Orders](
[OrderId] [uniqueidentifier] NOT NULL,
[OrderDate] [datetime] NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

–– change order date column to have a default of current date and time
ALTER TABLE dbo.Orders ADD CONSTRAINT
DF_Orders_OrderDate DEFAULT getDate() FOR OrderDate

–– test order date default value
insert into Orders (OrderId)
values (newId())

–– look at the awesome results
select *
from Orders
3/17/2009 4:32:49 PM EDT
[#2]
Thanks for the help, that's exactly what she was looking for!