[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

PamelaFoxcroft

3/30/2007 4:44:00 PM

Hi

I have a query which looks like this

select * from tablename where col1='test'

Sometimes in my queries I want to do this

select * from tablename where col2='test'

Now I want to wrap this in a stored procedure and I am curious as to
what is the best way to do this, like this:

create proc testproc(@parameter int =0)
as
if @parameter =0
select * from tablename where col1='test'
if @parameter=1
select * from tablename where col2='test'

Is there a more efficient way of doing this? The reason I am asking is
that right now we have two parameters so I will have to do the
following:

create proc testproc(@parameter int =0, @parameter2 int =0)
as
if @parameter =0 and @parameter2=0
select * from tablename where col1='test' and col2='test'
if @parameter=0 and @parameter2=1
select * from tablename where col2='test'
if @parameter=1 and @parameter2=0
select * from tablename where col1='test'

is there a slick way of handling this?

1 Answer

Tom Cooper

3/30/2007 5:01:00 PM

0

You can find a good discussion of ways to handle this sort of situation at
http://www.sommarskog.se/dyn-s...

Tom

"PamelaFoxcroft" <PamelaFoxcroft@gmail.com> wrote in message
news:1175273041.548527.223750@n76g2000hsh.googlegroups.com...
> Hi
>
> I have a query which looks like this
>
> select * from tablename where col1='test'
>
> Sometimes in my queries I want to do this
>
> select * from tablename where col2='test'
>
> Now I want to wrap this in a stored procedure and I am curious as to
> what is the best way to do this, like this:
>
> create proc testproc(@parameter int =0)
> as
> if @parameter =0
> select * from tablename where col1='test'
> if @parameter=1
> select * from tablename where col2='test'
>
> Is there a more efficient way of doing this? The reason I am asking is
> that right now we have two parameters so I will have to do the
> following:
>
> create proc testproc(@parameter int =0, @parameter2 int =0)
> as
> if @parameter =0 and @parameter2=0
> select * from tablename where col1='test' and col2='test'
> if @parameter=0 and @parameter2=1
> select * from tablename where col2='test'
> if @parameter=1 and @parameter2=0
> select * from tablename where col1='test'
>
> is there a slick way of handling this?
>