Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why setting current identity value is not working for me in SQL Server 2008 R2?

I am working with SQL Server 2008 R2.

I have a table seq_audit which has an identity column. This is the definition of the table:

CREATE TABLE [dbo].[seq_audit]
(
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [value] [bit] NULL,

    PRIMARY KEY CLUSTERED ([id] ASC)
          WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
                IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, 
                ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

The table is empty and never has had any rows in it before.

To check its current identity value, I ran this command:

DBCC CHECKIDENT (seq_audit, NORESEED) 
GO

And this is the result I get:

Checking identity information: current identity value 'NULL', current column value 'NULL'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

I want to set its current identity value to 15953711. So I ran this command:

DBCC CHECKIDENT (seq_audit, RESEED, 15953711)
GO

And this is the result I get:

Checking identity information: current identity value 'NULL', current column value '15953711'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

I thought it worked so I again check its current identity by running this command:

DBCC CHECKIDENT (seq_audit, NORESEED) 
GO

But I was not expected the result I get:

Checking identity information: current identity value 'NULL', current column value 'NULL'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

So somehow the setting of current identity value is not working. Why? What am I doing wrong here?

like image 710
ChumboChappati Avatar asked Dec 30 '25 01:12

ChumboChappati


2 Answers

This is caused by the fact that your table is empty. Try adding a single record and then everything will work. I have tried this and can confirm that it works.

Also, if you use SQL Server Management studio you can use the design feature to change the seed values. and manually add and delete records.

like image 133
Peter Smith Avatar answered Dec 31 '25 16:12

Peter Smith


https://msdn.microsoft.com/es-es/library/ms176057(v=sql.120).aspx

DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value )

Current identity value is set to the new_reseed_value. If no rows have been inserted into the table since the table was created, or if all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value.

Also why you dont start the seed on the create table IDENTITY?

Sql Fiddle Demo

CREATE TABLE [dbo].[seq_audit](
    [id] [bigint] IDENTITY(15953711,1) NOT NULL,
    [value] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY];
like image 31
Juan Carlos Oropeza Avatar answered Dec 31 '25 16:12

Juan Carlos Oropeza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!