Solved

Update table from another table  - including new records

Posted on 2009-05-11
6
254 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now