SQL更改表字段為自增標識


下面是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

推薦:http://www.cnblogs.com/roucheng/p/mssqlindex.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM