Case with exists with innerjoin in update query

Posted on 2016-07-20
Last Modified: 2016-07-20
Here is some sample data from 2 tables I have

Table 1  


Table 2

CustomerID     MailFlag

I am trying to write an update query that will :

update the Customer flag to 'T' on Customer Order table If the Customer Number exists in the Mail Customer table.  

update the Customer flag to 'F' on Customer Order table If the Customer Number does not exist in the Mail Customer table.  

Given data above table below should look like below after query


CustomerID     MailFlag
12345                T
66666                F

Was thinking of using case with inner join but not sure how
Question by:johnnyg123
  • 2
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 41721826
this is the syntax principles i would suggest

update co
 set mailflag = case when exists( Select * from customer c where c.customerid = co.customerid ) the 'T' else 'F' end
from customerorder co


update co
 set mailflag = case when c.customerid is not null then 'T' else 'F' end
from customerorder co
left join customer c on  c.customerid = co.customerid
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41721828
and of course make sure you have a index on customerorder.customerid to get the update done fast

Author Comment

ID: 41721838
Great! Thanks!

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server JOIN Question - which type to use 3 24
SQL Error in WHERE Clause 5 42
grouping logic 6 49
SQL Server Question 5 28
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

867 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

15 Experts available now in Live!

Get 1:1 Help Now