Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select all records from table1 and Insert into table2 where not exists in table2

Posted on 2004-08-20
2
Medium Priority
?
993 Views
Last Modified: 2008-03-17
Hi,

I have two tables......... (please see below)

Both tables have a unique index on the "trec_code" and "matter_no" fields.

What I want to do is select all the records from "Table1" that do not exist in "Table2", and then insert those records into "Table2".

How do I achieve this???

Thanks,
Steve.

-------------------------------

Table1:
trec_code matter_no  
--------- -----------
das       10000
das       20000
hpm       10000
hpm       20000
mjw       10000
mjw       20000
wdg       10000
wdg       20000

Table2:
trec_code matter_no  
--------- -----------
das       20000
wdg       10000

-------------------------------
0
Comment
Question by:ive5005s
[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 Comments
 
LVL 2

Accepted Solution

by:
nexusSam earned 400 total points
ID: 11850622
Try:

INSERT Table2
SELECT * FROM Table1 t1
WHERE NOT EXISTS
   (SELECT 1 FROM Table2 t2 WHERE t1.trec_code=t2.trec_code
    AND t1.matter_no=t2.matter_no)
0
 

Author Comment

by:ive5005s
ID: 11850697
Cheers.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

715 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