Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i need some help here.. am trying to implement a search result

conditions

1# it should fetch records that equals what keyword user enter on textbox(@SEARCH)

2# Along with the first condition It also fetch the matched records which is entered on textbox(@SEARCH)

3# What keyword searched that should show 1st and other matches will show after that( what i means priority queue)

4# It should check @loc paramater and fetch to that keyword.

SQL
@SEARCH NVARCHAR(100) = NULL,
@loc NVARCHAR(100) = NULL
as
select *
from tblBusinessCategory as b
    inner join tblUser as u on b.BusinessID=u.BusinessCategoryId
    inner join tblAddress as a on u.AddressId=a.AddressID
where b.BusinessName = @SEARCH and
a.City = @loc
       OR a.State = @loc
       and  b.BusinessName LIKE '%' + @SEARCH + '%'
Posted

If i understand you well...
Replace this:
SQL
where b.BusinessName = @SEARCH and
a.City = @loc
       OR a.State = @loc
       and  b.BusinessName LIKE '%' + @SEARCH + '%'

with:
SQL
where (b.BusinessName LIKE '%' + @SEARCH + '%' and (a.City = @loc OR a.State = @loc))


unless you want to fetch data when one of possible conditions is meet:
SQL
where ((b.BusinessName = @SEARCH and a.City = @loc)
       OR (a.State = @loc and  b.BusinessName LIKE '%' + @SEARCH + '%'))


[EDIT]
As to the OP's comments... i would suggest to create dynamic queries. See:
Building Dynamic SQL In a Stored Procedure[^]
Using Dynamic Queries[^]

[EDIT 2]
Assuming that you want to display data in ascending order, you have to add ORDER BY clause:
SQL
SELECT *
FROM TableName
WHERE <condition>
ORDER BY FieldName
 
Share this answer
 
v4
Comments
Member 11367931 20-Aug-15 5:27am    
If i give keywords in both the text boxes the result is fetching correctly. But if i gave the keyword in textbox(@search) alone the result is not fetching
Maciej Los 20-Aug-15 5:43am    
Did you ever heard about dynamic queries?
Member 11367931 20-Aug-15 5:44am    
No sir
Maciej Los 20-Aug-15 5:45am    
See updated answer (after edit).
Member 11367931 20-Aug-15 6:04am    
am dead at this Stored Procedure.. can u help me sir ??
SQL
select *
from tblBusinessCategory as b
    inner join tblUser as u on b.BusinessID=u.BusinessCategoryId
    inner join tblAddress as a on u.AddressId=a.AddressID
where a.City = @loc OR a.State = @loc
       OR b.BusinessName LIKE '%' + @SEARCH + '%'
Order By case when b.BusinessName = @SEARCH then 0 else 1 end



this is what i need..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900