Simple question but I'm a newbie. I have existing orders table 'Orders'(table1) and it has an OrderNumber as a column and the orders are number up 156. I have a existing table called 'orders'(table 2) from another website. I have to insert these orders from table 2 into table one and the OrderNumber for the inserted new order has to be 157....and so on for all orders coming in. I have asked this question and get answers that make no sense. Anyone?


I got it to work but why did it increment with 50? It went from 156 to 206. I only want it to take the next number in sequence.


It incremented by 50 because you added "ordID" to @idmax and ordID = 50.

Maybe you wanted an incrementing number using row_number?

SELECT @idmax = MAX(OrderNumber) FROM OrderNumbers

INSERT INTO OrderNumbers

 row_number() over(order by ordID) +@idmax,

 FROM orders

 SELECT * FROM OrderNumbers
| improve this answer | |
  • Worked Perfectly. Thank you @Mister Magoo – Fred Jul 16 '15 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.