Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help With Inserting New Records Only

Posted on 2013-12-04
5
Medium Priority
?
203 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
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

916 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