Solved

Update table from another table  - including new records

Posted on 2009-05-11
6
259 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
[X]
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
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Current Month Filter in Visual Studio 10 38
SQL Recursion schedule 13 34
Stored Procedure needs owner to execute 5 44
sql update 2 37
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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