有时候在线的数据结构发生了变化,先备份以前的数据,然后再用一个脚本进行初始化。但是在删除带默认值的列的时候,脚本老是报错,google一下,这样就可以了:
USE MyTestDB
--先删除约束
declare @name varchar(20)
select @name = b.name from sysobjects b join syscolumns a on
b.id = a.cdefault where a.id = object_id('UserInfo') and a.name = 'NickName'
exec('alter table UserInfo drop constraint ' + @name)
--然后再删除字段就OK了
ALTER TABLE dbo.UserInfo DROP COLUMN NickName
declare @name varchar(20)
select @name = b.name from sysobjects b join syscolumns a on
b.id = a.cdefault where a.id = object_id('UserInfo') and a.name = 'NickName'
exec('alter table UserInfo drop constraint ' + @name)
--然后再删除字段就OK了
ALTER TABLE dbo.UserInfo DROP COLUMN NickName