문제

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL):

ALTER TABLE         orders
ADD CONSTRAINT  default_date
DEFAULT         DATEADD (DAY,10,required_date) FOR required_date

Halp!

Edit: I've also tried this now:

ALTER TABLE         orders
ALTER COLUMN        required_date
ADD CONSTRAINT      required_date_plus_ten
DEFAULT             DATEADD (DAY,10,required_date)

Edit: Thanks to ypercube & my classmate. The final code is:

ALTER TABLE       orders
ADD CONSTRAINT    default_date
DEFAULT           (DATEADD (DAY,10,'required_date')) FOR required_date;
도움이 되었습니까?

해결책

The syntax in SQL-Server, for adding a DEFAULT value to an existing column is:

ALTER TABLE     orders
ADD CONSTRAINT  required_date_plus_ten
DEFAULT         DATEADD(day, 10, GETDATE())
FOR             required_date ;

Tested in SQL-Fiddle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top