Insert into a row at specific position into SQL server table with PK

前端 未结 6 1093
抹茶落季
抹茶落季 2020-12-16 23:58

I want to insert a row into a SQL server table at a specific position. For example my table has 100 rows and I want to insert a new row at position 9. But the ID column whic

6条回答
  •  醉酒成梦
    2020-12-17 00:30

    Usually you do not want to use primary keys this way. A better approach would be to create another column called 'position' or similar where you can keep track of your own ordering system.

    To perform the shifting you could run a query like this:

    UPDATE table SET id = id + 1 WHERE id >= 9

    This do not work if your column uses auto_increment functionality.

提交回复
热议问题