Question

This does not work:

DBCC CHECKIDENT('[DMS].[dbo].[ImportedFiles]', RESEED, 0)

Once the next record gets inserted, I want the seed to be 0 not 1.

I tried this but the function doesn't like it:

DBCC CHECKIDENT('[DMS].[dbo].[ImportedFiles]', RESEED, -1)

DBCC CHECKIDENT('[DMS].[dbo].[ImportedFiles]', RESEED, '-1')

How do I reset the seed of a table so that the next record that goes in is a ZERO? (0)?

Was it helpful?

Solution

If the IDENTITY column is defined as (0,1), and the table is empty, and there are no foreign keys involved, just issue:

TRUNCATE TABLE dbo.ImportedFiles;

If the table has foreign keys - or in any event, for that matter - you can just override the value:

SET IDENTITY_INSERT dbo.ImportedFiles ON;
INSERT dbo.ImportedFiles(col1, col2) SELECT 0, 'UNKNOWN';
SET IDENTITY_INSERT dbo.ImportedFiles OFF;

I have confirmed that @Avarkx's answer also works - reseed with -1 should yield a next insert of 0. So I'm not sure what version you're using where this doesn't work (I have no idea how to interpret "doesn't like it").

OTHER TIPS

I can't replicate this... Here's a test that works, SQL Server 2012. 2008 R2.

Also, @Anybody saying 0 means something, it really doesn't. While it's fairly common to see 0 values being used as unknowns and the like, business logic built around an IDENTITY column is an anti-pattern which generally should be avoided and definitely never recommended.

CREATE TABLE dbo.Foo
(
    Foo_PK BIGINT IDENTITY( 1, 1 ) NOT NULL,
    Bar BIT
);

INSERT INTO dbo.Foo (Bar)
    SELECT b.Bar
    FROM 
        (SELECT Bar = NULL
         UNION ALL  
         SELECT Bar = 1
         UNION ALL  
         SELECT Bar = 0) b;

SELECT *
FROM dbo.Foo;

DBCC CHECKIDENT ('dbo.Foo', RESEED, -1);

INSERT INTO dbo.Foo (Bar)
    SELECT  b.Bar
    FROM 
        (SELECT Bar = NULL
         UNION ALL  
         SELECT Bar = 1
         UNION ALL  
         SELECT Bar = 0) b;

SELECT *
FROM dbo.Foo;
GO

DROP TABLE dbo.Foo;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top