Solved

SUBQUERY or CASE STATEMENT or something

Posted on 2012-04-05
5
694 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Building a cohesive image for your brand is vital to making an impression on consumers. When the economy is tough, brands do better than unbranded  products. This can have a huge impact on your long-term profits, as the economy goes up and down.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

685 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