Solved

Help With Inserting New Records Only

Posted on 2013-12-04
5
191 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

18 Experts available now in Live!

Get 1:1 Help Now