John Porter
asked on
Select the next available record in a SQL table based on last record in another table
Hello Experts,
I have two SQL Tables that look like this:
Tbl 1:
Type Wt
1 10.0
2 10.2
3 11.3
Tbl 2:
Type Wt
1 10.0
2 10.2
3 11.3
7 22.5
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
From Tbl2
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??
Thanks!
I have two SQL Tables that look like this:
Tbl 1:
Type Wt
1 10.0
2 10.2
3 11.3
Tbl 2:
Type Wt
1 10.0
2 10.2
3 11.3
7 22.5
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
From Tbl2
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??
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
re:> You just need to change the '=' to '>':
In that case,it will insert all from Tbl_2 (maybe this is what is what Saxitalis wants):
Tbl 2:
Type Wt
1 10.0
2 10.2
3 11.3
7 22.5
8 12.0
12 14.3
In that case,it will insert all from Tbl_2 (maybe this is what is what Saxitalis wants):
Tbl 2:
Type Wt
1 10.0
2 10.2
3 11.3
7 22.5
8 12.0
12 14.3
Not all but everything greater than 3 (in this example).
I was just adding the next equal or greater than "@LastTypPlusOne + 1" as Saxitalis puts it). This is what I was trying to achieve. But when I read it again, I see my mistake: "I want to add new records in Tbl 2 to Tbl1 based on the last record in Tbl1."
ASKER
Yup - I tried that first Victor but records would not sort correctly so I was trying to import one record at a time.
I finally realized the reason records would not sort was because I was converting the column in question to VARCHAR data type when accessing via a linked server via C# code. I solved this bypopulating the table coming in through the linked server into a temp table with INT data type. Then performed the query on the temp table.
I am excepting your solution because it made me go back and think about "why"
Thanks!
John
I finally realized the reason records would not sort was because I was converting the column in question to VARCHAR data type when accessing via a linked server via C# code. I solved this bypopulating the table coming in through the linked server into a temp table with INT data type. Then performed the query on the temp table.
I am excepting your solution because it made me go back and think about "why"
Thanks!
John
Open in new window