Solved

How to write a SQL Server statement to insert records into 1 table from another using SQL Server 2005?

Posted on 2011-09-02
3
213 Views
Last Modified: 2012-05-12
I am working with SQL Server 2005.
I have 2 SQL Server tables named tblNew and tblCum.
These tables have identical data structures and field names.
The first field in the tables is named MailID and is unique.
How would you write a SQL Server statement to insert records into table tblCum from table
tblNew where MailID in tblNew is not in table tblCum?
 
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 250 total points
ID: 36473991
INSERT INTO tblCum
SELECT * FROM tblNew WHERE tblNew.MailID NOT IN (SELECT a.MailID FROM tblCum a)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36473994
try:

insert into tblCum
(select * from tblNew n where not exists (select 1 from tblCum c where c.mailid=n.mailid))
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
ID: 36474007
oops, (..) not needed


insert into tblCum
select * from tblNew n where not exists (select 1 from tblCum c where c.mailid=n.mailid)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

18 Experts available now in Live!

Get 1:1 Help Now