Executing SQL queries, but automatic addition starts at 0

I have a table in which there is an identifier column that I am trying to execute. Repositional work (I think), but when a new data item is inserted into the table, the identifier column starts at 0.

My code to re-copy:

CHECKIDENT DBCC (MyTable, RESEED, 0)

Identification specifications for tables:

  • Personality increment = 1
  • Identity Seed = 1

QUICK NOTE I perform a table deletion before visiting again

Please, help

+3
source share
3 answers

, "" ( , ), 0, . . 0 , 0, 1.

, :

-- Script to create a test table
IF EXISTS(SELECT 1 FROM Information_Schema.Tables WHERE TABLE_SCHEMA = 'dbo' 
            AND TABLE_NAME = 'SeedTest') BEGIN

    DROP TABLE SeedTest

END

-- Create a Test Seed Table
CREATE TABLE [dbo].[SeedTest](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Value] [varchar](255) NOT NULL,
 CONSTRAINT [PK_SeedTest] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


-- When a table is truncated or "Not Initialized" (meaning no data EVER inserted)
-- An initial reseed of 0 will make the first identity insert value = 0.
DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO

-- If you truncate the table and reseed the same effect will occur (first identity insert value = 0).
TRUNCATE TABLE SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')

SELECT * FROM SeedTest


-- When Deleting records from a table (Foreign key constraints may prevent a truncate)
-- Reseeding to 0 will set the last seed to 0 and make the next seed = 1
DELETE FROM SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO
+6

, :

DBCC CHECKIDENT(MyTable, RESEED, -1)

, , , . , 0.

, , :   DBCC CHECKIDENT (MyTable, RESEED, 0)

+3

, IDENTITY, 0 .

(IDENTITY(1,1)), , DBCC.

1 , :

DBCC CHECKIDENT(MyTable, RESEED, 1)

100, :

DBCC CHECKIDENT(MyTable, RESEED, 100)

, DBCC CHECKIDENT, , IDENTITY .

MSDN, :

DBCC CHECKIDENT 
( 
    table_name
        [ , { NORESEED | { RESEED [ ,new_reseed_value ] } } ]
)

new_reseed_value

.

, IDENTITY - 0, , 0 - , , ...

+1

Source: https://habr.com/ru/post/1752875/


All Articles