這在SQL Server 2008中不起做用: 數據庫
ALTER TABLE Employee ALTER COLUMN CityBorn SET DEFAULT 'SANDNES'
錯誤是: spa
關鍵字「 SET」附近的語法不正確。 code
我究竟作錯了什麼? orm
ALTER TABLE [dbo].[Employee] ADD DEFAULT ('N') FOR [CityBorn]
嘗試執行如下命令; it
ALTER TABLE Person11 ADD CONSTRAINT col_1_def DEFAULT 'This is not NULL' FOR Address
正確的方法以下: io
運行命令: table
sp_help [table name]
複製CONSTRAINT
的名稱。 form
刪除DEFAULT CONSTRAINT
: 語法
ALTER TABLE [table name] DROP [NAME OF CONSTRAINT]
運行如下命令: 方法
ALTER TABLE [table name] ADD DEFAULT [DEFAULT VALUE] FOR [NAME OF COLUMN]
在兩種狀況下,能夠更改列的默認值,
詢問
create table table_name ( column_name datatype default 'any default value' );
在這種狀況下,個人SQL Server不容許修改現有的默認約束值。 所以,要更改默認值,咱們須要刪除現有的系統生成或用戶生成的默認約束。 以後,能夠爲特定列設置默認值。
請遵循如下步驟:
執行此係統數據庫過程,它將表名做爲參數。 它返回表中全部列的全部約束的列表。
execute [dbo].[sp_helpconstraint] 'table_name'
句法:
alter table 'table_name' drop constraint 'constraint_name'
句法:
alter table 'table_name' add default 'default_value' for 'column_name'
歡呼@ !!!
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