Need to add constraint: date plus 10 days

做~自己de王妃 提交于 2020-01-04 03:24:08

问题


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;

回答1:


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



来源:https://stackoverflow.com/questions/13536376/need-to-add-constraint-date-plus-10-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!