Is there a way to tell SQL server to use specific default seed value for IDENTITY columns - assuming the (to be run) CREATE TABLE statements do not specify one? I don't really care about altering existing data or altering the seed values for specific IDENTITY columns. I want the same seed for all newly created identity columns. Assume I cannot modify the individual CREATE TABLE statements in any way.
views:
27answers:
1
+2
A:
the syntax is:
IDENTITY [ ( seed , increment ) ]
You must specify both the seed and increment or neither. If neither is specified, the default is (1,1). There is no way to set a different global default other than the (1,1). The only way to set a value other than (1,1) is to code it on the column.
here is the documentation: IDENTITY (Property)
EDIT
Based on this command:
DBCC CHECKIDENT ( 'table_name', RESEED, new_reseed_value )
You could use this script to reset all identity columns to the same @new_reseed_value
value:
DECLARE @Tables table (RowNumber int identity(1,1) primary key, TableCommand sysname)
DECLARE @new_reseed_value int
,@CurrentRow int
,@MaxRows int
,@CurrentTableCommand nvarchar(4000)
SET @new_reseed_value=9876
INSERT INTO @Tables
(TableCommand)
SELECT
'DBCC CHECKIDENT ( ''['+SCHEMA_NAME( OBJECTPROPERTY( OBJECT_ID, 'SCHEMAID' ))
+'].['+OBJECT_NAME( OBJECT_ID )+']'', RESEED, '+COALESCE(CONVERT(varchar(10),@new_reseed_value),'1')+' ) '
FROM SYS.COLUMNS
WHERE COLUMNPROPERTY(OBJECT_ID, NAME, 'IsIdentity') = 1
--AND exclude some tables here ifnecessary
SELECT @MaxRows=@@ROWCOUNT,@CurrentRow=0
WHILE @CurrentRow<@MaxRows
BEGIN
SELECT @CurrentTableCommand=''
,@CurrentRow=@CurrentRow+1
SELECT
@CurrentTableCommand=TableCommand
FROM @Tables
WHERE RowNumber=@CurrentRow
--PRINT @CurrentTableCommand
EXEC(@CurrentTableCommand)
END
KM
2010-03-19 17:26:42