Solved

Insert Into for New Data Only

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

     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 …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

758 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