garyinmiami2003
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.
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.
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.
If what you have is the way I must go, then ok.
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
update TableX
set Col_a = case when exists (SELECT TYPE FROM TABLEF WHERE ANYCOLUMN = 'Y') then 'X' else 'Z' end
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window