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

zimmer9
zimmer9 used Ask the Experts™
on
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?
 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
INSERT INTO tblCum
SELECT * FROM tblNew WHERE tblNew.MailID NOT IN (SELECT a.MailID FROM tblCum a)
HainKurtSr. System Analyst

Commented:
try:

insert into tblCum
(select * from tblNew n where not exists (select 1 from tblCum c where c.mailid=n.mailid))
HainKurtSr. System Analyst
Commented:
oops, (..) not needed


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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial