Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Update table from another table  - including new records

Posted on 2009-05-11
6
Medium Priority
?
263 Views
Last Modified: 2012-05-06
update xfer
  set custid = c.custid
from xfer
join myDB2.dbo.customers c
  on xfer.email = c.emailaddress

-won't update xfer with new customers, will it?
How can you update xfer with new customers also?
0
Comment
Question by:fmsol
  • 2
  • 2
  • 2
6 Comments
 
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 x.email = c.emailaddress
0
 

Author Comment

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

Accepted Solution

by:
Aneesh Retnakaran earned 500 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 )
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 22

Expert Comment

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

MERGE INTO xfer
USING myDB2.dbo.customers c
ON xfer.email = c.emailaddress
WHEN MATCHED THEN UPDATE
SET custid = c.custid
WHEN NOT MATCHED THEN INSERT
(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.




0
 

Author Comment

by:fmsol
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 *)
0
 
LVL 22

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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