Solved

SUBQUERY or CASE STATEMENT or something

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Taking long time 7 481
Cannot create ODBC connection over the network 3 1,227
Sybase initialize new disk in Solaris 10 failed 2 950
install ASE 16 side by side with ASE 15.7 15 854
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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