Solved

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

Posted on 2004-08-20
2
977 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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.
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…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

622 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