Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help With Inserting New Records Only

Posted on 2013-12-04
5
Medium Priority
?
199 Views
Last Modified: 2013-12-05
I have raw data imported into a table from a flat file.

I want to take that data and insert records only that are new based using two columns as my references. If those two aren't new then the record is not updated.

Any ideas on what method is the best/easiest for this?
0
Comment
Question by:sonofstimpy1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 39695417
Create a temp table and import the records first into it
You can then use NOT EXISTS

insert into ABC
select *
from ##temptable A
where not exists(select 1 from ABC B where A.someid = B.someid)
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39695423
>only that are new based using two columns as my references
What is your definition of 'new'?

If this is a staging table, typically the table is deleted before the import, hence all rows are 'new'.

If this is a production table where there are existing rows that need to stay, a common method would be to have 'auditing' columns such as CreatedBy, CreatedDate, where the CreatedDate has a GETDATE() inserted into it, such that identifying rows 'just imported' is as easy as getting the latest CreatedDate.

If the definition of 'new' is something else you'll need to spell it out for us.
0
 

Author Comment

by:sonofstimpy1
ID: 39696523
Just using generic TABLE1 and TABLE2

They're both the same data with the same data fileds
Let's say TABLE 1 adds 5 new records.
And I really don't want to drop TABLE 2.
I'm looking for something that will see that there are 5 new records and insert those records if 2 of say 10 fields are unique (call them memberid and date).

Is this making more sense?
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 39698143
insert into TABLE1
select *
from TABLE2 A
where not exists(select 1 from TABLE1 B where A.memberid = B.memberid and A.date = B.date)
0
 

Author Closing Comment

by:sonofstimpy1
ID: 39699557
That's exactly what I was looking for.

Thanks
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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