Select the next available record in a SQL table based on last record in another table
Posted on 2014-11-10
I have two SQL Tables that look like this:
I want to add new records in Tbl 2 to Tbl1 based on the last record in Tbl1.
The following code works fine as long as "Type" values are sequential in Tbl2.
@LastTypPlusOne = Select MAX(Type) From Tb_1
Insert Into Tbl1(Type, Wt)
Select Type, Wt
Where Type = @LastTypPlusOne + 1
(The record with a "Type" value of 7 will not Insert...)
-- The last Type value in Tbl1 is 3
-- There is no Type value 4, 5 or 6 in Tbl2
-- The next new record (has Type value of 7) in Tbl2 needs to be inserted into Tbl1 based on last Type value in Tbl1.
I would like to Insert all new records from Tbl2 into Tbl1 based on the "Type" value in the last record in Tbl1.
Does anyone know how to do this??