Solved

Insert Into for New Data Only

Posted on 2013-05-20
4
200 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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 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…

751 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