Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 48
sql server insert 12 30
SQL Query - Database name 'TempDB' ignored, referencing object in tempdb - Error 2 13
SQL Count issue 24 15
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

773 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