Hi, Im using SP with 2 parameters named @criteria and @value. The @criteria serve as column flag in which column to perform the search eg Supplier and Invoice No and the @value which is the query value.
It seems CASE has the flexibility to meet the requirement but didnt work for me.
<code>
select column1
from myview
where
case when (@criteria='a') then
column1=@value
else
column2=@value
end
</code>
It looks like you want to create your sql statement dynamically.
Do a search for dynamic SQL and hopefully that will help you.
Also, what database are you using? SQL Server or Oracle?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz' save a blobFileStreamDataTable To Text Filemy blog
Hi abhijit, that was an option I consider but I found something interesting and its looks very promising. It does work when I test but not sure with the performance though compare to dynamic sql.
select column1
from myview
where
case @criteria
when 'a' then case column1=@value then 1 else 0 end
when 'b' then case column2=@value then 1 else 0 end
end=1
If @criteria='a'
Begin
select column1
from myview
where column1=@value
End
Else
Begin
If @criteria='b'
Begin
select column1
from myview
where column2=@value
End
End
Creating a "super-conditional" based SELECT statement makes no sense. It does not run any faster (probably slower) - is a nightmare to modify later...
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
I live by the rule that CASE should be avoided in a WHERE clause. Pretty much just to keep the execution plans "more sql-standard".
Look at this simple pair of queries - and the attached execution plan - as you can see the first one does a INDEX SEEK (good) - the second does an INDEX SCAN (not so good). Notice the INDEX SCAN is twice as "costly" as the SEEK. If the fields involved are not in an index it's most likely going to do a TABLE SCAN (really, really not so good).
Code:
Select * From Concept_T Where CId between 10 and 20
Select * From Concept_T Where Case When CId between 10 and 20 Then 1 Else 0 End=1
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
An obscure body in the SK system. The inhabitants call it Earth
Posts
7,957
Re: Using WHERE CASE in SP
I've often heard the "No Case in a Where clause" argument and have always accepted it at face value.
Just wondering, though, why not have this entirely handled in the Where clause but using Ands and Ors rather than Cases:-
Code:
Select Blah
From BlahTable
Where (@Criteria = 'a' and Column1 = @Value)
OR (@Criteria = 'b' and Column2 = @Value)
OR (@Criteria = 'c' and Column3 = @Value)
etc
That would seem like a more "set based" aproach to me where as ifs and elses feel more "procedural". I've done absolutely zero performance testing around that though.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
@funky - I've often used the method you indicate - just a standard WHERE clause with proper use of OR's to handle the major conditional differences.
Although - when the "very fields" you are going to be using for "evaluation" are "conditional" I can see having separate SELECT's.
I also find myself - for the purposes of large SPROCS returning data for reporting - setting up a TEMP TABLE or TABLE VARIABLE with PRIMARY KEY's to include. I can massage and manipulate that TEMP TABLE several times before arriving at the SINGLE SELECT at the bottom of the SPROC that pulls FROM #TEMPTABLE and JOIN's in all the others.
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
An obscure body in the SK system. The inhabitants call it Earth
Posts
7,957
Re: Using WHERE CASE in SP
Although - when the "very fields" you are going to be using for "evaluation" are "conditional" I can see having separate SELECT's
Ok, I can certainly see that from a readibility and maintenance perspective. Are you aware of any performance implications to either aproach? I just want to make sure I'm not missing a trick.
Personally I use my aproach just because it's valid for just about any query. I don't want to have performance tune every single query I write so I just aim for a consistent aproach and then tune anything that's problematic. But I always want to know if there's a known exception case I can accomodate instead of waiting to fix it after the event.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd