下面是SQL語句:
--刪除主鍵約束 DECLARE @Constraint_Name varchar (200) select @Constraint_Name = Name from dbo.sysobjects where Xtype = 'PK' and Parent_Obj = (select [ID] from dbo.sysobjects where id = object_id(N'[表名稱]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) if @Constraint_Name <> '' begin alter table 表名稱 drop constraint @Constraint_Name end --刪除主鍵字段 -- 何問起 hovertree.com declare @count int select @count = count(*) from sysobjects a,syscolumns b where a.id = b.id and b.name = '主鍵字段名' and a.Name = '表名稱' if @count > 0 begin alter table 表名稱 drop column 主鍵字段名 end --創建字段,主鍵,並自增 alter table 表名稱 add 主鍵字段名 int identity(1,1) PRIMARY KEY