Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 19th, 2008, 02:15 AM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Search Condition

Hi This is madhavi

am working with a project with ASP.NET Using VB.NET..

i have requirement that i have to provide the result based on search condition....

First : For Serach i have to search based on given CITY and CATEGORY....



      For this i have written a StoredProcedure like:

************************************************** ************************************************** **************

         Create PROCEDURE YellowPages_Search
(
@city nvarchar(50),
@SearchWord nvarchar(200),
)
AS

declare @sql nvarchar(1000)
set @sql='select * from YellowPages_Userdetails where city='''+@city + '''and (category like ''%' + @SearchWord + '%'' or subcategory like ''%' + @SearchWord + '%'') '

exec(@sql)

GO

************************************************** ************************************************** ************************************************** *******************

Now i want to extend this search condition for LOCATION and SUBCATEGORY

 means my search condition should include CITY , LOCATION , CATEGORY and SUBCATEGORY

(here the location and subcategory may be given or may not be given)
means the search should be based on
  1: city=something and category/subcategory=something
  2: city=something and ctegory=something and location=nothing and category=nothing
  3: city=something and ctegory=something and location=something and category=nothing
  4:city=something and ctegory=something and location=something and category=something
  5:city=something and ctegory=something and location=nothing and category=something


so please help me out

Thanks in Advance,

Madhavi



 
Old February 19th, 2008, 05:19 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

You can use optional argument
Create PROCEDURE YellowPages_Search
(
@city nvarchar(50),
@SearchWord nvarchar(200),
@is_category bit= null,
@is_subcategory bit= null,
@is_location bit= null,

)
AS

declare @sql nvarchar(1000)

set @sql='select * from YellowPages_Userdetails where city='''+@city + '''
if isnull(@is_category,0)<>0
    set @sql=@sql+ ' and (category like ''%' + @SearchWord + '%'' or subcategory like ''%' + @SearchWord + '%'') '
if isnull(@is_location,0)<>0
    set @sql=@sql+' and (location like ''%' + @SearchWord + '%'') '

and so on.
Here I am confused with columns u have written, also i want to know there is only one searchphrase or you want to accept separate searchword for separate columns

You can directly pass appropriate value, for eg. instead of location flag dircectly pass locationvalue and check if it is not null then append to queryexec(@sql)



urt

Help yourself by helping someone.
 
Old February 21st, 2008, 03:13 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi madhavi

For this, you need not to build dynamic sql queries. It can be achieved by using normal sql statment
please refer to the below procedure,which tells the SQL statment to take only those param where are passed and neglect others from "where" condition

So here you can pass any combination of the params as needed and pass null to which it is not needed.

exec YellowPages_Search 'bangalore', null, null -- this tells to search only for city
exec YellowPages_Search 'bangalore', null, 'mg road'-- this tells to search only for city and location
exec YellowPages_Search null, 'cyber cafe', 'mg road' -- this tells to search only category and sub category and location
exec YellowPages_Search 'chennai', 'cyber cafe' -- this tells to search only on city and category and sub category

and so on.. you can make query more strong if have more search criteria and columns in that table.

give a try, hope this helps

sp:

create procedure YellowPages_Search
(
    @city nvarchar(50) = null,
    @SearchWord nvarchar(200) = null,
    @location nvarchar(50) = null
)
as
begin
    set @SearchWord = '%' + @SearchWord + '%'
    select
        *
    from
        YellowPages_Userdetails
    where
        (nullif(@city, '') is null or city = @city)
        and (nullif(@SearchWord, '') is null or (category like @SearchWord or subcategory like @SearchWord))
        and (nullif(@location, '') is null or location = @location)
end

With Regards,
Raghavendra Mudugal





Similar Threads
Thread Thread Starter Forum Replies Last Post
or condition kgoldvas XSLT 1 July 31st, 2007 03:44 AM
NEWBIE Question: Expected "" in search condition aasiddle SQL Language 5 March 19th, 2007 03:49 PM
NEWBIE Question: Expected "" in search condition aasiddle ASP.NET 2.0 Basics 0 March 4th, 2007 11:21 AM
Help PLZ..Condition for Search Page shopgirl Classic ASP Databases 4 April 5th, 2004 10:59 PM
WHERE BETWEEN search condition in sproc jtyson SQL Server 2000 1 August 7th, 2003 09:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.