Solved

cae and select together

Posted on 2009-07-13
5
177 Views
Last Modified: 2012-05-07
is it possible to use CASE and select together... For example

select CASE WHEN @Spec_Desc = 'All' THEN

(SELECT     ID from TBL) ELSE .............
0
Comment
Question by:Mr_Shaw
  • 3
5 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 350 total points
ID: 24839186
Sure..
You can use it like that and an example below.
Make sure that SELECT statement is enclosed in braces to work.
DECLARE @a int;
SET @a = 1;
SELECT CASE WHEN @a = 1 THEN (SELECT 'Pass') ELSE (SELECT 'Fail') END 

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24839191
you can't have "SELECT" statement in CASE THEN statement. CASE THAN should be nested in SELECT.
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 150 total points
ID: 24839210
I am sorry, disregard my previous answer, you can use select but it shouldn't return more than one value and there shouldn't be more than one column in select, you can use TOP 1 clause.
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 150 total points
ID: 24839216
something like this:

DECLARE @a int;
SET @a = 1;
SELECT CASE WHEN @a = 1 THEN (SELECT Top 1 ID from tbl where name='anything') ELSE (SELECT 'Fail') END

0
 

Author Closing Comment

by:Mr_Shaw
ID: 31602799
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

775 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