asim jan

asim jan

  • NA
  • 24
  • 28.7k

Creating a custom search stored procedure

Feb 10 2012 11:28 AM
hi i am building a online book store for which i want to provide search functionality
there is a text box a dropdown n search button

text box is used to input search string
drop down is used to select  by auhtor/by category/ by title

i am able to search the bookdetaisl table thorugh a string based search for whihc i get the string from text box.. returning results irrespective of the value from drop down list..
i pass three parameters to stored procedure comapring the three values from the same textbox with the three columns in my table i.e author,bookname,category


this is general stored procedure where the string is taken from search bar it has nothign to do whether
any value from dropdown is selected or not..

USE [Book_Store]
GO
/****** Object:  StoredProcedure [dbo].[searchbook]  Script Date: 02/10/2012 21:40:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[searchbook]
  @book_name varchar(40) = NULL,
  @author varchar(40) = NULL,
  @category varchar(40) = NULL
 
 
AS
SELECT book_name,
  author ,
  category
 
FROM book_details
WHERE (@book_name IS NULL OR book_name LIKE '%' + @book_name + '%')
  or (@author IS NULL OR author LIKE '%' + @author + '%')
  or (@category IS NULL OR category LIKE '%' + @category + '%')


for allowing by author search / by book title search / by category search.
we will adopt following stategy
first select value from drop down whether by author/ by category / by title
then display accordingly
eg if drop down is by author
we show only those books titles that exist in the table for thatn authr

similarly if dropdown is by category
we show only those books titles that exist in the table for that category column


please let me know how to make it work
this is my snapshot


Answers (3)