-1

I have a SQL Server 2016 table and it has than 10,000 rows. The Id column is a sequential id incremented by 1. But that Id is not in the correct order as shown in the screenshot below where 330 is missing. Is there any way to correct this sequence in an existing table?

enter image description here

I've tried like below but it's not working.

CREATE SEQUENCE contacts_seq
AS BIGINT
   START WITH 1
   INCREMENT BY 1
   MINVALUE 1
   MAXVALUE 99999
   NO CYCLE
   CACHE 10;
gotqn
  • 42,737
  • 46
  • 157
  • 243
TechGuy
  • 4,298
  • 15
  • 56
  • 87

1 Answers1

1

There is no point of fixing this - you are going to perform a operation which will block the access to your table and may affect your application performance.

And then, someone can delete the record X and you will be at the same position again.

Generally, the perpouse of id is to uniquely identify a row. If for some visualization reasons you do not want to have gaps you can use RANKING functions to fix this.

For example:

SELECT ROW_NUMBER () OVER (ORDER BY id) AS [ID]
gotqn
  • 42,737
  • 46
  • 157
  • 243