Question

CREATE TABLE [dbo].[tbl_Person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Address] [varchar](50) NULL,
[Phone] [varchar](50) NULL,
 CONSTRAINT [PK_tbl_Person] PRIMARY KEY CLUSTERED 
(
    [PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,     
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

In this table when inserting value from application using stored procedure with query

INSERT INTO [dbo].[tbl_PersonName]
       ([Name])
 VALUES
       ('name')

the 'PersonID' column got unusual value. The value jumps randomly like, from 1896 to 11896 and again jumps from 11905 to 21905. i worry if such jumps occurs the range could be insufficient and the data seems quite unorganized. I wonder if those unused value would be reused or not? After inserting some data what should i do to re managed the sequence? i am worried with the issue, this didn't happen in 2010 though i used the same pattern of code as in 2012. help me!

enter image description here

enter image description here

Was it helpful?

Solution

That is because you are trying to insert a new column to your primary key column.

If you have a table lets say with 1000 rows and you delete the record at 797 position. Then when you will insert a new row to your table then the value will be inserted at 1001 position not at 797.

I wonder if those unused value would be reused or not?

No the values will not be reused. As whenever you delete a column from the table and then try to insert the value again in the table then the value will be inserted to a new position.

EDIT:-

Also you may find this helpful.

Microsoft added sequences in SQL Server 2012

By default when you create a SEQUENCE you can either supply CACHE size. Caching is used to increase performance for applications that use sequence objects by minimizing the number of disk IOs that are required to generate sequence numbers.

To fix this issue, you need to make sure, you add a NO CACHE option in sequence creation / properties like this.

CREATE SEQUENCE TEST_Sequence
    AS INT
    START WITH 1
    INCREMENT BY 1
    MINVALUE 0
    NO MAXVALUE
   NO CACHE

However if you want the same old behavior then you may use trace flag 272

Since the trace flag 272 option worked for OP. Here are the steps to add trace flag 272:-

  1. Open "SQL Server Configuration Manager"
  2. Click "SQL Server Services" on the left pane
  3. Right-click on your SQL Server instance name on the right pane
  4. Click "Properties"
  5. Click "Startup Parameters"
  6. On the "specify a startup parameter" textbox type "-T272"
  7. Click "Add"
  8. Confirm the changes
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top