搜索结果存储过程

时间:2015-09-02 09:01:56

标签: sql-server sql-server-2008 stored-procedures

我有2个文本框,一个用于资源,另一个用于位置。

截至目前我所做的是,我创建了一个用于显示搜索结果的存储过程。我的问题是在提供资源的同时获取相应的资源,同时也获取与位置匹配的其他资源

例如:

如果我在印度搜索沙龙,它会显示位于(印度)的结果,并显示医院,学校等。

假设:

textbox 1: Saloon
textbox 2: India

如果我这样给出我的搜索结果

搜索结果

Resource : Naturals - India's No: 1
Location : India

Resource : Apollo Hospitals
Location : India

Resource : Sahyadri School
Location : India

如何解决这个问题?

存储过程是:

@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 a.City = @loc OR a.State = @loc
       OR b.BusinessName LIKE '%' + @SEARCH + '%'
Order By case when b.BusinessName = @SEARCH then 0 else 1 end

1 个答案:

答案 0 :(得分:0)

尝试将其添加到以下位置:

where (@SEARCH IS NULL or (b.BusinessName LIKE '%' + @SEARCH + '%'))
  and (@loc IS NULL or (a.City = @loc OR a.State = @loc))

简单例子here

相关问题