set nocount on
declare @tables table (
id int identity,
tablename sysname,
columnName sysname
)
declare @counter int
declare @count int
declare @newseed nvarchar(20)
declare @tablename sysname
declare @columnname sysname
declare @sql varchar(max)
declare @sqlex varchar(max)
declare @sqlMax nvarchar(max)
declare @sqlMaxEx nvarchar(max)
DECLARE @pvalue VARCHAR(250)
DECLARE @param NVARCHAR(250)
set @sql = 'DBCC CHECKIDENT (''@table'', RESEED, @value)'
set @sqlMax = 'SELECT @value=MAX(@pkfield) FROM @table'
SET @param = '@value VARCHAR(250) OUTPUT'
insert into @tables
select DISTINCT TABLE_NAME, COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
select @count = @@ROWCOUNT
set @counter = 1
while @counter <= @count begin
select @tablename = tablename, @columnname = columnName
from @tables
where id = @counter
set @sqlMaxEx = REPLACE(REPLACE(@sqlmax, '@table', @tablename), '@pkfield', @columnname)
print @sqlmaxex
EXECUTE SP_EXECUTESQL @sqlMaxEx, @param, @value = @pvalue OUTPUT
select @newseed = ISNULL(cast(@pvalue as int), 0) + 1
set @sqlex = REPLACE(REPLACE(@sql, '@table', @tablename ), '@value', @newseed)
print @sqlex
--exec (@sqlex)
set @counter = @counter + 1
end
|