Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically in a bid to lower the amount of code I have im trying to combine my search query into one procedure instead of 3.

Users can search reports by Title,Description or Author.
The current procedure looks something like this


	(
	@SearchQuery NVARCHAR(100),
	@FolderID INT,
	@SearchType NVARCHAR(100)
	)
AS
BEGIN
	SELECT 
	ReportDetailsID,
        Name

	FROM 
	Reports
	WHERE

	FolderID = @FolderID AND
		
	CASE @SearchType
	WHEN 'Name' THEN Name  LIKE '%'+@SearchQuery+'%' 
	WHEN 'Desc' THEN Description  LIKE '%'+@SearchQuery+'%' 
	WHEN 'Author' THEN CreatedBy  LIKE '%'+@SearchQuery+'%' 
	
END 


The idea was that I run the procedure and give it a string value of what kind of search it is I want to search by and the final where statement is based off that choice

How do I do this kind of statement properly? My attempt was based off similar SQL but they didn't have the like element to the problem
Posted

You could include all options in the SQL and use boolean expressions to turn off the ones you don't need:

SQL
<br />
SELECT ...<br />
FROM ...<br />
WHERE<br />
    ( @SearchType = 'Name' AND Name LIKE '%'+@SearchQuery+'%' )<br />
    OR<br />
    ( @SearchType = 'Desc' AND Description LIKE '%'+@SearchQuery+'%' )<br />
    OR<br />
        ...<br />


Nick
 
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