Michael Miner Michael Miner - 1 year ago 122
SQL Question

Increment Variable in SQL

I have a table which does not have any auto increments. I have to insert data into this table, each time incrementing the ID by 1000.

So I have

SELECT @maxId = Max(ID)
(@maxId + 1000, DATA),
(@maxId + 2000, DATA),
(@maxId + 3000, DATA)

Instead of explicitly incrementing the ID for each insert, is there a way to have it 'auto-increment'. I can not use one of the
or something like that, simply because the ID's are created in a weird way

Answer Source

There is nothing stopping you from doing the following and getting the data inserted correctly.

insert into table1(ID,DATA) 
VALUES ((select max(id) from table1 as T) +1000, DATA), 
       ((select max(id) from table1 as T) +1000, DATA);

Or is it something else that you meant?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download