SQL - 4 Variables?

Soldato
Joined
17 Jul 2005
Posts
3,193
Hi Guys,

I'm pretty new when it comes to SQL and only know the basics.

I want a query that does the following.

I've got a page with four drop down boxes containing various variables. The default entry in the job down box is blank.

The user should be able to select one of the boxes, or more if they wish ... press the button which executes the SQL and returns the result.

Say I did an SQL query that;

select * from x where (var1 = @var1) AND (var2 = @var2) up to var 4

Can I pass a wildcard(?) in the =@ variable that I pass to the query if the user does not select anything in one of the boxes?

e.g...

select * from x where (var1 = *)

Hope i'm being clear :-S

Thanks
Steve
 
you can do
select * from x where var1 like @var1

and have the @var1 contain '%'
which should match anything.

That sounds like it would do the job perfectly... however when I try and preview the query inputting % I get...

Failed to convert parameter value from a string to an Int32. Is it a different wildcard for integers?
 
(cast(user_id as varchar(8)) LIKE @user_id)

Allows me to use the % wildcard search by casting the int to a varchar and it works a treat.

If anyone can point out any flaws in the above that'd be great.

Thanks for the help Simon :)
 
I see. I'm not too concerned about the efficiency as it's not a massive database.

I guess an alternative would have been to create an if statement on the application side to choose to execute an appropriate query based upon the input.... although that would have meant creating four seperate queries in my table adapter.... ?

Thanks anyway, seems to be running as intended.
 
Back
Top Bottom