使用SQL 进行数据同步后,自增长列的数据只是进行了数据同步,但是对应的Seed并没有增长。

 

如果只有个别表,可以使用

DEclARE @autoid int  
set @autoid=(SELECT MAX(autoid) FROM mysqlpub)  
if @autoid is NULL 
  SET @autoid=0
DBCC CHECKIDENT (mysqlpub, RESEED,@autoid)

来更新自增长列当前的Seed。

 

如果含有表比较多,可以结合游标批量操作

 
declare @tableName varchar(50)
declare @colName varchar(20)
declare @SQL varchar(2000)
declare tmpCursor cursor
 for  select o.name,c.name from sysobjects o, sys.columns c
 where o.id=c.object_id and o.xtype='U'  and o.name like 'tap%' and is_identity=1
 
 open tmpCursor
 fetch next from  tmpCursor into @tableName,@colName
 while @@FETCH_STATUS = 0
 begin
  SET @SQL = 'DEclARE @autoid int ' + CHAR(13)
   + ' set @autoid=(SELECT MAX(IDENTITYCOL)+1 FROM '+@tableName+') '  + CHAR(13)
   + 'if @autoid is NULL  '+ CHAR(13)
   +'  SET @autoid=10000 '+ CHAR(13)
   + ' DBCC CHECKIDENT ('+@tableName+', RESEED,@autoid)'+ CHAR(13)
 EXEC(@SQL)
 --SELECT @SQL
 fetch next from  tmpCursor into @tableName,@colName
end
close tmpCursor
deallocate tmpCursor

posted on 2010-12-17 23:08  沉默的心  阅读(1809)  评论(0编辑  收藏  举报