这在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”附近的语法错误。
我做错了什么?
当前回答
试试下面的命令;
ALTER TABLE Person11
ADD CONSTRAINT col_1_def
DEFAULT 'This is not NULL' FOR Address
其他回答
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
如果一个限制已经存在,它的默认名称是:
-- Drop existing default constraint on Employee.CityBorn
DECLARE @default_name varchar(256);
SELECT @default_name = [name] FROM sys.default_constraints WHERE parent_object_id=OBJECT_ID('Employee') AND COL_NAME(parent_object_id, parent_column_id)='CityBorn';
EXEC('ALTER TABLE Employee DROP CONSTRAINT ' + @default_name);
-- Add default constraint on Employee.CityBorn
ALTER TABLE Employee ADD CONSTRAINT df_employee_1 DEFAULT 'SANDNES' FOR CityBorn;
ALTER TABLE tblUser
ADD CONSTRAINT DF_User_CreatedON DEFAULT GETDATE() FOR CreatedOn
ALTER TABLE [dbo].[Employee] ADD DEFAULT ('N') FOR [CityBorn]
ALTER TABLE Employee ADD DEFAULT 'SANDNES' FOR CityBorn