Link to home
Start Free TrialLog in
Avatar of garyinmiami2003
garyinmiami2003Flag for United States of America

asked on

SUBQUERY or CASE STATEMENT or something

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.
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Update YourTableA

set COL_A =  'X'

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

Open in new window

Avatar of garyinmiami2003

ASKER

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.
Avatar of alpmoon
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
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?
ASKER CERTIFIED SOLUTION
Avatar of alpmoon
alpmoon
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial