cae and select together

is it possible to use CASE and select together... For example

select CASE WHEN @Spec_Desc = 'All' THEN

(SELECT     ID from TBL) ELSE .............
Mr_ShawAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Raja Jegan RConnect With a Mentor SQL Server DBA & ArchitectCommented:
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
 
RiteshShahCommented:
you can't have "SELECT" statement in CASE THEN statement. CASE THAN should be nested in SELECT.
0
 
RiteshShahConnect With a Mentor Commented:
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
 
RiteshShahConnect With a Mentor Commented:
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
 
Mr_ShawAuthor Commented:
thanks
0
All Courses

From novice to tech pro — start learning today.