Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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 .............
0
Mr_Shaw
Asked:
Mr_Shaw
  • 3
3 Solutions
 
Raja Jegan RSQL 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
 
RiteshShahCommented:
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
 
RiteshShahCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now