Solved

Case with exists with innerjoin in update query

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

Table 1  
MailCustomer

CustomerID
12345
56768


Table 2
CustomerOrder

CustomerID     MailFlag
12345
66666

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

CustomerOrder

CustomerID     MailFlag
12345                T
66666                F


Was thinking of using case with inner join but not sure how
0
Comment
Question by:johnnyg123
[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
3 Comments
 
LVL 143

Accepted Solution

by:
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

or

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
0
 
LVL 143

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
0
 

Author Comment

by:johnnyg123
ID: 41721838
Great! Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

691 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