SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is there any database setting that affects CHECKIDENT(RESEED)


Is there any database setting that affects CHECKIDENT(RESEED)

Author
Message
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7855 Visits: 7146
rwitt 95744 (5/7/2014)
I think placing this in my test data script will resolve the issue (or rather, cover it, for both situations):

DECLARE @DELETEDFROM_<tablename> bit = 1
IF (SELECT IDENT_CURRENT('<tablename>') = 1 AND (SELECT COUNT(*) FROM <tablename>Wink = 0
SET @DELETEDFROM_<tablename> = 0 -- New table, has never contained any rows
-- Need to run the above prior to clearing the table

-- Run this after clearing the table
IF @DELETEDFROM_<tablename> = 0
BEGIN
-- New table, has never contained rows; CHECKIDENT(RESEED) treats it as a TRUNCATEd table
DBCC CHECKIDENT ('<tablename>', reseed, 1)
END
ELSE
BEGIN
-- Table exists & was DELETEd FROM; need to use different RESEED value
DBCC CHECKIDENT ('<tablename>', reseed, 0) -- Needed b/c DELETEd rather than TRUNCATEd this table
END


For efficiency, rather than counting all the rows, simply check to see if one exists:


IF (SELECT IDENT_CURRENT('<tablename>') = 1 AND NOT EXISTS(SELECT TOP (1) * FROM <tablename>Wink



SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15019 Visits: 18595
Thinking back, I cannot remember ever using 0 as seed value for a CHECKIDENT if the initial seed value was <> 0, regardless of which method was used of emptying the table.
Cool
rwitt 95744
rwitt 95744
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 149
CHECKIDENT(RESEED) is quirky, based on TRUNCATE/DELETE & whether the table is a brand-new (never-contained-any-rows) table or an already-existing table.
CHECKIDENT(RESEED,0) acts differently, based on the above. If it's a brand-new table, then, yes, it will start the key count at key = 0 (1st actually assigned key will be 0.) However, if it's an existing table that's been deleted from (not truncated), it will actually start the key count at key = 1 (1st actually assigned key will be 1.)
I need to delete from (not truncate) the table, due to foreign keys. So, the 2nd example (CHECKIDENT(RESEED,0)) restarts my keycount at 1.
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15019 Visits: 18595
What about dropping and recreating the constraints?
Cool
rwitt 95744
rwitt 95744
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 149
Could do that, prefer this approach.
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15019 Visits: 18595
I normally prefer to script the creation of all constraints and use something like this procedure to drop them when needed
Cool
CREATE PROCEDURE [dbo].[DROP_SCHEMA_CONSTRAINTS]
(
@TABLE_SCHEMA NVARCHAR(128)
)
AS
/*
DECLARE @TABLE_SCHEMA NVARCHAR(128) = N'myschema'
EXEC dbo.DROP_SCHEMA_CONSTRAINTS @TABLE_SCHEMA
*/

DECLARE @SQL_STR NVARCHAR(MAX) = N'';

SELECT @SQL_STR = (SELECT N'ALTER TABLE '
+ T.TABLE_SCHEMA + N'.'
+ T.TABLE_NAME + N' DROP CONSTRAINT '
+ T.CONSTRAINT_NAME + N' -- ' + T.CONSTRAINT_TYPE + N'
'
FROM
(
SELECT OBJECT_NAME(OBJECT_ID) AS CONSTRAINT_NAME,
SCHEMA_NAME(schema_id) AS TABLE_SCHEMA,
OBJECT_NAME(parent_object_id) AS TABLE_NAME,
type_desc AS CONSTRAINT_TYPE
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND type_desc NOT LIKE 'PRIMARY_KEY%'
) AS T
WHERE T.TABLE_SCHEMA = @TABLE_SCHEMA
FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)');
EXEC (@SQL_STR);


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search