Hi. Sorry if it is a silly question.
I want to build a conditional WHERE clause for a stored procedure.
My procedure has some attributes like
@param1
@param2
These params correspond to filters in my web application. If users select an item from a ListBox, the application will populate these params with the corresponding key for filtering the field. But if users DO NOT select a filter in these list boxes, leaving like -- ALL --, I don't want the stored procedure to filter them. In a very poor way (I know this is not the correct syntax), I will give an example:
PROCEDURE XXX
@param1 int,
@param2 int
AS
SELECT * FROM Table1
IF @Param2 <> 0 THEN (0 would correspond to ALL)
WHERE field1 = @Param1 AND field2 = @Param2
ELSE
WHERE field1 = @Param1
==== Filter for @PAram2 would only work if user passed a valid key for it. Otherwise, application would pass a '0' that the PROCEDURE would understand as a message not to filter.
How can I do that??
I want to build a conditional WHERE clause for a stored procedure.
My procedure has some attributes like
@param1
@param2
These params correspond to filters in my web application. If users select an item from a ListBox, the application will populate these params with the corresponding key for filtering the field. But if users DO NOT select a filter in these list boxes, leaving like -- ALL --, I don't want the stored procedure to filter them. In a very poor way (I know this is not the correct syntax), I will give an example:
PROCEDURE XXX
@param1 int,
@param2 int
AS
SELECT * FROM Table1
IF @Param2 <> 0 THEN (0 would correspond to ALL)
WHERE field1 = @Param1 AND field2 = @Param2
ELSE
WHERE field1 = @Param1
==== Filter for @PAram2 would only work if user passed a valid key for it. Otherwise, application would pass a '0' that the PROCEDURE would understand as a message not to filter.
How can I do that??