code_junkie

renumber primary key

Nov 11th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. SET IDENTITY INSERT [MyTable] ON
  2.  
  3. SET IDENTITY INSERT [MyTable] OFF
  4.  
  5. SELECT Field1, Field2 INTO #MyTable FROM MyTable
  6.  
  7. TRUNCATE TABLE MyTable
  8.  
  9. INSERT INTO MyTable
  10. (Field1, Field2)
  11. SELECT Field1, Field2 FROM #MyTable
  12.  
  13. SELECT * FROM MyTable
  14. -----------------------------------
  15. ID Field1 Field2
  16. 1 Value1 Value2
  17.  
  18. -- Reset the primary key counter
  19. dbcc checkident(ErrorCode, reseed, 7000)
  20.  
  21. -- Move all rows greater than 8000 to the 7000 range
  22. insert into ErrorCode
  23. select Description from ErrorCode where ErrorCodeID >= 8000
  24.  
  25. -- Delete the old rows
  26. delete ErrorCode where ErrorCodeID >= 8000
  27.  
  28. -- Reset the primary key counter
  29. dbcc checkident(ErrorCode, reseed, 8000)
Add Comment
Please, Sign In to add comment