Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert Into for New Data Only

Posted on 2013-05-20
4
Medium Priority
?
204 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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