hi all
i've got two tables called "webusers" (id, name, fk_country) and "countries" (id, name)at the meantime, i've a search-page where i can fill a form to search users. in the dropdown to select the country i included an option which is called "all countries".
now the problem is: how can i make a stored procedure that makes a restriction to the fk_country depending on the submitted fk_country parameter?
it should be something likeSELECT * FROM webusers
(if @fk_country > 0, which is the value for "all countries")
{
WHERE fk_country = @fk_country
}who has an idea how to solve this problem?
![]() |
0 |
![]() |
Hi,
Look at the sample below and let me know
SELECT
*
FROM
webusers
WHERE
@fk_country = 0
OR @fk_country = fk_country
Zafar Iqbal
VP Technology
Hansvits
** Please mark as ANSWER if my reply helped you
![]() |
0 |
![]() |
if(@fk_Country = 0)
set @fk_Country = null
select * from WebUsers
where fk_Country = isnull(@fk_Country,fk_Country)
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
ramireddyindia:
if(@fk_Country = 0)
set @fk_Country = null
select * from WebUsers
where fk_Country = isnull(@fk_Country,fk_Country)To escape extra processing why not simply:
IF (@fk_Country IS NULL)
BEGIN
SELECT * FROM WebUsers
END
ELSE
BEGIN
IF (@fk_Country > 0)
SELECT * FROM WebUsers
WHERE fk_Country = @fk_Country
END
![]() |
0 |
![]() |
Try in your stored procedure
IF @fk_country IS NULL SET @fk_country = 0
IF @fk_country = 0
SELECT * FROM webusers ORDER BY NAME -- Select all
ELSE
SELECT * FROM webusers WHERE fk_country = @fk_country
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
![]() |
0 |
![]() |
What the first "IF" line is needed for? :-)TATWORTH:
Try in your stored procedure
IF @fk_country IS NULL SET @fk_country = 0
IF @fk_country = 0
SELECT * FROM webusers ORDER BY NAME -- Select all
ELSE
SELECT * FROM webusers WHERE fk_country = @fk_country
![]() |
0 |
![]() |
SergeyS, In your below query, what happens if the user pass @fk_country value as zero, it doesn't return any results. Please Verify It.
IF (@fk_Country IS NULL)
BEGIN
SELECT * FROM WebUsers
END
ELSE
BEGIN
IF (@fk_Country > 0)
SELECT * FROM WebUsers
WHERE fk_Country = @fk_Country
ENDAnd one more point,
KinqDario Said that he will send the value 0, if user select the Allcountries in dropdownlist.
if he select any particular country, then he will send the CountryId.
So Never he send null to storedprocedure.so always your query never goes into if condition.
it always go into else condition only.
if you check the timecomplexity, it will always execute 3 instructions in your query.
BUt if you check below query, it will execute 3 instructions in one case, and 2 instructions in another case.if(@fk_Country = 0)
set @fk_Country = null
select * from WebUsers
where fk_Country = isnull(@fk_Country,fk_Country)
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
ramireddyindia:
SergeyS, In your below query, what happens if the user pass @fk_country value as zero, it doesn't return any results. Please Verify It.
IF (@fk_Country IS NULL)
BEGIN
SELECT * FROM WebUsers
END
ELSE
BEGIN
IF (@fk_Country > 0)
SELECT * FROM WebUsers
WHERE fk_Country = @fk_Country
ENDAnd one more point,
KinqDario Said that he will send the value 0, if user select the Allcountries in dropdownlist.
if he select any particular country, then he will send the CountryId.
So Never he send null to storedprocedure.so always your query never goes into if condition.
it always go into else condition only.Yes, I see now he wrote:
I thought "NULL" was for "all" countries. But it seems like he probably meant 0 for "all" countries. So, in this case my query should be:(if @fk_country > 0, which is the value for "all countries")" .IF (@fk_Country > 0)
SELECT * FROM WebUsers
WHERE fk_Country = @fk_Country
ELSE
SELECT * FROM WebUsersif you check the timecomplexity, it will always execute 3 instructions in your query.
BUt if you check below query, it will execute 3 instructions in one case, and 2 instructions in another case.if(@fk_Country = 0)
set @fk_Country = null
select * from WebUsers
where fk_Country = isnull(@fk_Country,fk_Country)Actually, the declarative T-SQL syntax could not be interpreted in the number of instructions that way. Basically my query states
- check if CountryId is supplied
- if yes, then get all the records containing this CountryId value in CountryId field
- if no, then just grab all the records
No processing on each row is performed, except for very fast fetching on the primary key in the case when CountryId is supplied.
On your query always "where fk_Country = isnull(@fk_Country,fk_Country)" processing will ocur even if what we need is just to grab all the records.
![]() |
0 |
![]() |
The first IF is to convert null values to 0. It thus simplifies the second IF
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
![]() |
0 |
![]() |
TATWORTH:
The first IF is to convert null values to 0. It thus simplifies the second IFIF @fk_country IS NULL SET @fk_country = 0
IF @fk_country = 0
SELECT * FROM webusers ORDER BY NAME -- Select all
ELSE
SELECT * FROM webusers WHERE fk_country = @fk_countryAs far as I understand, the above is equal to
IF (@fk_country = 0) OR (@fk_country IS NULL)
SELECT * FROM webusers ORDER BY NAME -- Select all
ELSE
SELECT * FROM webusers WHERE fk_country = @fk_country
![]() |
0 |
![]() |