Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to make a search app. where user select a specific column for searching then searching take palce in that column now i have 10 column for searching.
Do i need to make 10 different searching query ??? or a general query can work for all.
remember user can select more than one column for searching
suppose
user want to search in
Title
And
Description
And
Location

another user want to search like this
Title
or
Description
or
Location

another want like this
Title
or
Description

another user only want to search in Title

so what i need to do write query separately for each selection. which is really time consuming and tough. Can i make general query which do all kind of searching??
Posted

Use dynamic query. Make all parameter from sp nullable so it will not give error if input parameter is not provided.
e.g @Title Varchar(100) = null

then create dynamic query & add optional filter parameter by checking parameter is null or not.

Refer this link
Building Dynamic SQL In a Stored Procedure[^]
 
Share this answer
 
Comments
Maciej Los 1-Oct-13 5:33am    
Good one!
+5!
pradiprenushe 1-Oct-13 5:49am    
Thanks
Muhamad Faizan Khan 1-Oct-13 13:19pm    
What will i do with AND operation??
pradiprenushe 1-Oct-13 14:13pm    
How you are going to decide to use OR/And? What is logic? Or any case
Muhamad Faizan Khan 1-Oct-13 14:24pm    
a person want to search a specific work In title and Description. I will provide him and .or option definitely
Hi,

Please find the code sample for the search pattern you described.
SQL
/*
drop Table Books
Create Table Books
(
Bookid int identity(1,1), 
Title varchar(150),
Description varchar(150),
Location  varchar(150), 
isactive bit
)

Insert Books values('Around the world','Around the world 80 days', 'Chennai',1)
Insert Books values('Paradise Lost','Paradise lost by John Milton', 'London',1)
Insert Books values('War and peace','Leo Tolstoy', 'Russia',1)
Insert Books values('Chennai Travel Guide','Chennai Travel Guide', 'Chennai',1)
Insert Books values('Introduction to C# ','Microsoft', 'USA',0)


Select * from Books Where Title  = 'Around the world' and Location ='Chennai'
Select * from Books Where Title  = 'Around the world' or Location ='Chennai'
*/

/********* Dynamic Query ***************/
go
Declare @Qry nvarchar(max)
Declare @Title varchar(150)  
Declare @Description varchar(150) , @DescOpr varchar(10)
Declare @Location varchar(150) , @LocationOpr varchar(10)

Set @Title = 'Around the world' 
set @Description = ''
Set @Location = 'Chennai' 
set @LocationOpr = 'or'

Set @Qry =  ' Select * from Books  Where Isactive =1'
if (@Title <> '' ) 
Begin 
set @Qry =   @Qry + ' And Title = '''+ @Title + ''''
End
if (@Description <> '' and  @DescOpr ='AND') 
Begin 
set @Qry =   @Qry + ' And description ='''+  @Description + ''''
End
Else 
Begin 
set @Qry =   @Qry + ' or description ='''+  @Description + ''''
End


if (@Location <> '' and  @LocationOpr ='AND') 
Begin 
set @Qry =   @Qry + ' And Location = '''+ @Location + ''''
End
Else
Begin 
set @Qry =   @Qry + ' or Location = '''+ @Location + ''''
End
print @Qry
exec (@Qry)
go


Hope this helps
Regards,
Mahe...
 
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