Solved

different select based on case

Posted on 2007-03-22
9
260 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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

752 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