Solved

Update table from another table  - including new records

Posted on 2009-05-11
6
257 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 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 )
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

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.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

770 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