Update table from another table  - including new records

Posted on 2009-05-11
Last Modified: 2012-05-06
update xfer
  set custid = c.custid
from xfer
join myDB2.dbo.customers c
  on = c.emailaddress

-won't update xfer with new customers, will it?
How can you update xfer with new customers also?
Question by:fmsol
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
  • 2
  • 2
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24357570
that will update

you can also do the same with

update x
  set custid = c.custid
from xfer x
join myDB2.dbo.customers c
  on = c.emailaddress

Author Comment

ID: 24357704
Won't the join on = c.emailaddress exclude new records from the source?
In xfer the records are updated, but no new records appear
LVL 75

Accepted Solution

Aneesh Retnakaran earned 125 total points
ID: 24357747
the update operation is something you need to perform with existing records, in order to add newly created emails, put an insert statement too
insert into xRefer (CustID, Email )
SELECT CustID, Email
FROM myDB2.dbo.customers c
WHERE NOT EXISTS (SELECT 1 FROM xRefer  WHERE email = c.emailaddress )
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

LVL 22

Expert Comment

ID: 24357801
In SQL Server 2008 use the MERGE statement to do both UPDATE and INSERT:

USING myDB2.dbo.customers c
ON = c.emailaddress
SET custid = c.custid
(custid, email)
VALUES (c.custid, c.emailaddress);

In fact it's better to use MERGE in place of the UPDATE... FROM... JOIN method even if you don't want to do INSERTs as well (the INSERT part is optional). MERGE is much superior to Microsoft's proprietary UPDATE... FROM syntax because it is standard SQL, compatible with other DBMSs, more concise, generally more efficient and doesn't suffer from the nasty and unreliable "gotcha" that UPDATE does when the join criteria turns out to be non-unique. Avoid UPDATE... FROM and use MERGE instead.


Author Comment

ID: 24358701
Thank you aneeshattingal and dportas.
I see I still have a lot to learn...

Another thing: in the SET statement; do I have to list all fields that shall be updated, or is it an easier way to update all fields (like *)
LVL 22

Expert Comment

ID: 24359013
You have to list all the columns by name.

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
denied execute as 13 57
TSQL Assignining CTE column in to a variable 3 27
T-SQL: Need training video on creating a Clustered Index 2 58
store vs query adhoc - no show rows 4 37
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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