Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

758 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

14 Experts available now in Live!

Get 1:1 Help Now