sql修改自增长ID

ALTER PROCEDURE [sp_Table_SetIdentity] 
     @Table varchar(100) 
    ,@Schema    varchar(100) 
AS
BEGIN
    DECLARE @vSQL nvarchar(max) 
     
    -- drop PK on [SchoolID] 
    SET @vSQL = N' 
ALTER TABLE  #Schema#.#Table# 
    DROP CONSTRAINT PK_#Table#; 
'
    SET @vSQL = Replace(@vSQL, '#Table#', @Table) 
    SET @vSQL = Replace(@vSQL, '#Schema#', @Schema) 
print @vSql 
    EXEC (@vSql) 
     
    -- rebuild IDENTITY column 
    SET @vSQL = N' 
ALTER TABLE  #Schema#.#Table# 
    DROP COLUMN [SchoolID]; 
     
ALTER TABLE  #Schema#.#Table# 
    ADD [SchoolID] int IDENTITY(1, 1) NOT NULL; 
     
ALTER TABLE [#Schema#].[#Table#] ADD  CONSTRAINT [PK_#Table#] PRIMARY KEY CLUSTERED  
( 
    [SchoolID] ASC 
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = ON, ONLINE = OFF) ON [PRIMARY]; 
'
    SET @vSQL = Replace(@vSQL, '#Table#', @Table) 
    SET @vSQL = Replace(@vSQL, '#Schema#', @Schema) 
print @vSql 
    EXEC (@vSql) 
END

exec [sp_Table_SetIdentity] 'Area_School','dbo'

 

posted on 2016-03-21 16:52  以勤补拙  阅读(1511)  评论(0编辑  收藏  举报