这在SQL Server 2008中不起作用:
ALTER TABLE Employee ALTER COLUMN CityBorn SET DEFAULT 'SANDNES'
错误是:
关键字“SET”附近的语法错误。
我做错了什么?
这在SQL Server 2008中不起作用:
ALTER TABLE Employee ALTER COLUMN CityBorn SET DEFAULT 'SANDNES'
错误是:
关键字“SET”附近的语法错误。
我做错了什么?
当前回答
刚刚发现3个简单的步骤来改变已经存在的列,之前是空的
update orders
set BasicHours=0 where BasicHours is null
alter table orders
add default(0) for BasicHours
alter table orders
alter column CleanBasicHours decimal(7,2) not null
其他回答
试试下面的命令;
ALTER TABLE Person11
ADD CONSTRAINT col_1_def
DEFAULT 'This is not NULL' FOR Address
ALTER TABLE tblUser
ADD CONSTRAINT DF_User_CreatedON DEFAULT GETDATE() FOR CreatedOn
您可以使用以下语法,有关详细信息,请参阅此问题和答案:将具有默认值的列添加到SQL Server中的现有表中
语法:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES
例子:
ALTER TABLE SomeTable
ADD SomeCol Bit NULL --Or NOT NULL.
CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is
autogenerated.
DEFAULT (0)--Optional Default-Constraint.
WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.
另一种方法:
右键单击表并单击“设计”,然后单击要设置默认值的列。
然后在页面底部添加一个默认值或绑定:例如字符串为'1'或int为1。
Hoodaticus的解决方案是完美的,谢谢,但我也需要它是可重新运行的,并找到了这种方式来检查它是否已经完成……
IF EXISTS(SELECT * FROM information_schema.columns
WHERE table_name='myTable' AND column_name='myColumn'
AND Table_schema='myDBO' AND column_default IS NULL)
BEGIN
ALTER TABLE [myDBO].[myTable] ADD DEFAULT 0 FOR [myColumn] --Hoodaticus
END
刚刚发现3个简单的步骤来改变已经存在的列,之前是空的
update orders
set BasicHours=0 where BasicHours is null
alter table orders
add default(0) for BasicHours
alter table orders
alter column CleanBasicHours decimal(7,2) not null