Solved

different select based on case

Posted on 2007-03-22
9
263 Views
Last Modified: 2007-03-22
I have an SP that accepts a single input param.
I want to run a different select based on it's value.

create procedure blahblah
@type varchar(100)
as
select case @type
when 'all' then
    select etc
when 'unassigned' then
   select etc
else
   select etc
end case

but it's riddled with errors.
Syntax error or trying the impossible?
0
Comment
Question by:QPR
[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
  • 5
  • 3
9 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 18774792
you can't do that directly

create procedure blahblah
@type varchar(100)
AS

DECLARE @sql VARCHAR(8000)
SELECT @SQL  =
 case @type
when 'all' then
    'select etc'
when 'unassigned' then
   'select etc'
else
  'select etc'
end
EXEC @SQL
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18774877
The best way to accomplish this is as follows:

If @type = 'ALL'
begin
SELECT * FROM TableName
end
else
if @Type = 'A'
begin
SELECT * FROM TableName WHERE FieldName = 'SomeValue'
end
else
if @type = 'B'
begin
SELECT * FROM TableName WHERE FieldName = 'AnotherValue'
end

0
 
LVL 29

Author Comment

by:QPR
ID: 18774878
EXEC (@sql)

thanks
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18774883
There are other possibilies based upon your data...
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18774897
Actually... try to stay away from dynamic SQL if at all possible! You're killing performance. (either syntax :-) )
0
 
LVL 29

Author Comment

by:QPR
ID: 18774943
Actually I accepted the first answer as it worked and I half guessed it was the solution.
That said after then reading yours and weighing them both up I went with your solution... but don't tell anyone :)
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18775034
Should be so quick to accept a solution until you have several options to evaluate.
0
 
LVL 29

Author Comment

by:QPR
ID: 18775508
should or shouldn't?
As I said I had a feeling that the answer would be dynamic sql.
Saw the post, tried it and it worked.
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18776152
Shouldn't :-)  ... and there is almost always a better solution than using dynamic SQL.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

631 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