Solved

cae and select together

Posted on 2009-07-13
5
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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