Sql-server – Get new ID for inserted ID that comes from another table

sql serversql server 2014

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?

http://sqlfiddle.com/#!6/d423d/11/0

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.

Best Answer

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

Maybe you wanted an incrementing number using row_number?

DECLARE @idmax INT
SELECT @idmax = MAX(OrderNumber) FROM OrderNumbers

INSERT INTO OrderNumbers
(OrderNumber,
 OrderGUID)

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

 FROM orders



 SELECT * FROM OrderNumbers