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
Solved

Insert Into for New Data Only

Posted on 2013-05-20
4
198 Views
Last Modified: 2013-12-16
I have a table (tbl1) with 8 rows of data for Agents and their Sales
A - 3
B - 6
C - 12
D - 7
E - 8
F - 9
G - 10
H - 22

I want to insert data into a second table (tbl2), but only new data. So if tbl2 currently has Agents and Sales in it for Agents A & B, when I perform an insert I only want data for Agents C, D, E, F, and G to go into tbl2. Thus the data already in tbl2 for Agents A & B does NOT change.
I am not writing the correct Insert statement or Update, need help. Thanks
0
Comment
Question by:SeTech
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Kent Fichtner earned 500 total points
ID: 39181386
how are you going to be doing this insert?  is it going to be based on a trigger, or is it a one time thing that you are writing a query for?

you could try something like:

Update TBL2
SET <column> = <data here>
WHERE AGENT NOT IN (SELECT AGENT FROM TBL1)
0
 

Author Comment

by:SeTech
ID: 39181404
I will send the new data weekly, but how could you do an update when the data is NOT yet in tbl2?
0
 
LVL 4

Expert Comment

by:Kent Fichtner
ID: 39181429
I guess I am not quite understanding what you are looking to do.  Where are you getting the data for tbl2?  How are you going to be inserting it (through a stored procedure)?  As long as tbl2 has a column for agent, the were clause above should still be valid.  In the case of the first insert it would find no agents on tbl2 the same as tbl1 and would insert everything.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39181482
You would use NOT EXISTS.

INSERT INTO tbl2(col1, col2)
SELECT col1, col2
FROM tbl1
WHERE NOT EXISTS (
    SELECT 1
    FROM tbl2
    WHERE tbl2.col1 = tbl1.col1
)
;

Open in new window


Just adjust the WHERE clause of the subquery to be the correct columns that match an existing record in tbl2.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Copy Database Wizard 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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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