Solved

Help With Inserting New Records Only

Posted on 2013-12-04
5
194 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:ewangoya
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 65

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:
ewangoya earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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.

821 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