Solved

Select the next available record in a SQL table based on last record in another table

Posted on 2014-11-10
6
139 Views
Last Modified: 2014-11-12
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!
0
Comment
Question by:Saxitalis
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40434153
Would try this. This is done in SQL Server Express 2012; please make necessary adjustment if SS2005 different:
declare @LastTbl_1 int;
declare @NextTbl_2 int;
declare @MaxTbl_2 int;
declare @ExistsInTbl_2 int;
select @LastTbl_1 = max(Type) From Tbl_1;
select @MaxTbl_2 = max(Type) From Tbl_2;  -- used later to end the loop

set @LastTbl_1 =  @LastTbl_1 + 1;

While(@ExistsInTbl_2= 0) 
begin

  select @NextTbl_2 = Type From Tbl_2 Where Type = @LastTbl_1;

  if @NextTbl_2>0 
  begin  -- match is found

     Set @ExistsInTbl_2 = 1; -- to get out of the loop

	 Insert Into Tbl_1(Type, Wt)
      Select Type, Wt
      From Tbl_2
      Where Type = @NextTbl_2;

  end

	if @MaxTbl_2= @LastTbl_1
	   Set @ExistsInTbl_2 = 1; -- to get out of the loop
	else
	   Set @LastTbl_1 = @LastTbl_1 + 1;  -- add 1 to search for next
  
end

Open in new window

0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40434504
You just need to change the '=' to '>':
@LastTypPlusOne = Select MAX(Type) From Tb_1

 Insert Into Tbl1(Type, Wt)
 Select Type, Wt
 From Tbl2
 Where Type > @LastTypPlusOne

Open in new window

0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40435084
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
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40435088
Not all but everything greater than 3 (in this example).
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40435094
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."
0
 

Author Closing Comment

by:Saxitalis
ID: 40438947
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now