SQL 自增长列identity 数据同步问题,批量更新自增长列的Seed
时间:2010-12-17 来源:沉默的心
如果只有个别表,可以使用
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