Solved

Insert Into for New Data Only

Posted on 2013-05-20
4
201 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
[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
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 60

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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Backup & Restore 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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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