Solved

different select based on case

Posted on 2007-03-22
9
232 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
  • 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
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18774883
There are other possibilies based upon your data...
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now