Solved

SUBQUERY or CASE STATEMENT or something

Posted on 2012-04-05
5
697 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
[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
5 Comments
 
LVL 29

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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