如何修改列的默认值T-SQL

时间:2022-09-22 19:24:28

 

如何修改列的默认值T-SQL

没有办法直接通过脚本修改列的默认值。只能先删除原来默认值的约束,再重新添加默认值的约束

 

SELECT d.definition d.name,*
FROM sys.default_constraints AS d
INNER JOIN sys.columns AS c
ON d.parent_column_id = c.column_id
WHERE d.parent_object_id = OBJECT_ID(N'table name', N'U')
AND c.name = 'column name';

----
alter table [table name]
drop constraint [上面d.name]
go
alter table [table name]
add constraint 约束的名字 default 1 for column Name
go
exec sp_helpconstraint [table name]