Hello,
I've a query that can accept 6 variables that affect it's outcome. Currently the SQL string is built dynamically through an asp. So if the user selects values for COUNTRY and PRODUCT the string is built to look something like:
....where ProductID = @varProductID and CountryCode = @varCountryCode
I'm trying to create a stored procedure that will be able to handle all the possible variations in the WHERE clause but having some trouble.
I've tried something like:
declare @iCnty int, @iProd int
if @varProductID > 0 set @iprod = 1 else set @iprod = 0
if @varCountryCode > 0 set @iCnty = 1 else set @iCnty = 0
Then in the WHERE:
WHERE
((@iProd = 1 and ProductID = @varProductID) or (@iProd = 0 and ProductID > 0))
and
((@iCnty = 1 and CountryCode = @varCountryCode ) or (@iCnty = 0 and CountryCode > 0))
So if the user populates both ProductID and CountryCode I would expect the query to evaluate both "@var.." variables given the presence of @iprod/@icnty = 1.
If only @varProductID is populated I would expect it to return all records with this ProductID. The lack of a @varCountryCode should have no affect over the results; should be as if it wasn't part of the where clause at all.
I can get this logic to work if I'm only dealing with one variable but as soon as I add the second my query just seems to run for ever. I've not had a chance to see whether it would ever finish (when passed only a @varProductID) but it should only take 3 seconds to run.
Hopefully I explained this well enough. Thanks in advance for any input and assistance.
O.
I've a query that can accept 6 variables that affect it's outcome. Currently the SQL string is built dynamically through an asp. So if the user selects values for COUNTRY and PRODUCT the string is built to look something like:
....where ProductID = @varProductID and CountryCode = @varCountryCode
I'm trying to create a stored procedure that will be able to handle all the possible variations in the WHERE clause but having some trouble.
I've tried something like:
declare @iCnty int, @iProd int
if @varProductID > 0 set @iprod = 1 else set @iprod = 0
if @varCountryCode > 0 set @iCnty = 1 else set @iCnty = 0
Then in the WHERE:
WHERE
((@iProd = 1 and ProductID = @varProductID) or (@iProd = 0 and ProductID > 0))
and
((@iCnty = 1 and CountryCode = @varCountryCode ) or (@iCnty = 0 and CountryCode > 0))
So if the user populates both ProductID and CountryCode I would expect the query to evaluate both "@var.." variables given the presence of @iprod/@icnty = 1.
If only @varProductID is populated I would expect it to return all records with this ProductID. The lack of a @varCountryCode should have no affect over the results; should be as if it wasn't part of the where clause at all.
I can get this logic to work if I'm only dealing with one variable but as soon as I add the second my query just seems to run for ever. I've not had a chance to see whether it would ever finish (when passed only a @varProductID) but it should only take 3 seconds to run.
Hopefully I explained this well enough. Thanks in advance for any input and assistance.
O.