Skip to content
Advertisement

Insert rows into a table with a increment column without auto increment

I have a table Person with this structure:

I can’t use auto-increment on the ID column.

I have to insert 10 rows (which exist in @Persons table variable) into the Person table and I know current the max ID is 125.

How can I insert those 10 rows into the Person table without using a loop or cursor?

Advertisement

Answer

Get the maximum value and add it to a ROW_NUMBER() column during the select.

Example db fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement