Solved

SUBQUERY or CASE STATEMENT or something

Posted on 2012-04-05
5
675 Views
Last Modified: 2012-04-05
I need to set a char column called COL_A to 'X'  if the sub query below returns any rows:

SELECT TYPE FROM TABLEF WHERE  ANYCOLUMN = 'Y'.

iF IT DOES NOT RETURN ANY ROWS, I NEED TO LEAVE IT THE SAME VALUE:


So what I need to do in more english terms:

Set COL_A = 'X' when exists (SELECT TYPE FROM TABLEF WHERE  ANYCOLUMN = 'Y'.)
    else do not do anything to the column.
0
Comment
Question by:garyinmiami2003
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37812807
Update YourTableA

set COL_A =  'X'

From tableF where tableA.ID = tableF.ID and tableF. AnyColumn ='Y' 

Open in new window

0
 

Author Comment

by:garyinmiami2003
ID: 37812836
Well, this is not the solution I was hoping for.  I'm trying to update the value of a column in a Select rather than a seperate  operation.

If what you have is the way I must go, then ok.
0
 
LVL 13

Expert Comment

by:alpmoon
ID: 37812838
If you need to update another value in case of non-existence:

update TableX
set Col_a = case when exists (SELECT TYPE FROM TABLEF WHERE  ANYCOLUMN = 'Y') then 'X' else 'Z' end
0
 

Author Comment

by:garyinmiami2003
ID: 37812854
almost there

your code: else 'Z' end

I do not want the column changed if sub query returns nothing, so could I just say else Col_a?
0
 
LVL 13

Accepted Solution

by:
alpmoon earned 500 total points
ID: 37812861
You can do that way as well. But it is effectively the same with what sammySeltzer suggested

update TableX
from TableX
set Col_a = case when exists (SELECT TYPE FROM TABLEF WHERE  ANYCOLUMN = 'Y') then 'X' else Col_a end
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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

9 Experts available now in Live!

Get 1:1 Help Now