Solved

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

Posted on 2004-08-20
2
928 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
2 Comments
 
LVL 2

Accepted Solution

by:
nexusSam earned 100 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 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

13 Experts available now in Live!

Get 1:1 Help Now