troubleshooting Question

SQL Where statement containing freetext and case

Avatar of RobHedley
RobHedley asked on
DatabasesMicrosoft SQL Server 2005SQL
13 Comments1 Solution263 ViewsLast Modified:

I'm trying to create a stored procedure that has a list of parameters which are optional which then performs a select statement against inner joined tables.

All works fne until I try to add a free text statement and then i get stuck.

My question is how could i add a freetext query to the code below and make it optional? The freetext search has to cover multiple columns such as Duration, Salary, RoleTitle, RoleDescription, etc.

The keywords list for the freetext element of the query would be provided to the stored procedure through a @keywords parameter which would be a comma delimited nvarchar.


@UserID uniqueidentifier = NULL,
	@CountryID int = NULL,
	@VacancyPublished bit = NULL,
	@VacancyTypeID uniqueidentifier = NULL,
	@Sector uniqueidentifier = NULL,
	@SubSector uniqueidentifier = NULL,
	@Location nvarchar = NULL,
	@RecruiterRef nvarchar = NULL
SELECT     tblVacancies.VacancyID, tblVacancies.UserID, tblVacancies.SubSectorID, tblVacancies.CountryID, tblVacancies.Location, tblVacancies.DateAdvertised, 
                      tblVacancies.StartDate, tblVacancies.ApplicationClosingDate, tblVacancies.RecruiterRef, tblVacancies.VacancyTypeID, tblVacancies.Duration, tblVacancies.Salary, 
                      tblVacancies.RoleTitle, tblVacancies.RoleDescription, tblVacancies.RoleCandidateRequirements, tblVacancies.RoleAdditionalInfo, tblVacancies.VacancyPublished, 
                      tblRecruiters.RecruiterName, tblCountries.CountryName, tblVacancyTypes.VacancyTypeName, tblSubSectors.SubSectorName, tblSectors.SectorName
FROM         tblVacancies INNER JOIN
                      tblRecruiters ON tblVacancies.UserID = tblRecruiters.UserID INNER JOIN
                      tblCountries ON tblVacancies.CountryID = tblCountries.CountryID INNER JOIN
                      tblVacancyTypes ON tblVacancies.VacancyTypeID = tblVacancyTypes.VacancyTypeID INNER JOIN
                      tblSubSectors ON tblVacancies.SubSectorID = tblSubSectors.SubSectorID INNER JOIN
                      tblSectors ON tblSubSectors.SectorID = tblSectors.SectorID
WHERE     (tblVacancies.VacancyPublished = (CASE WHEN @VacancyPublished IS NULL THEN tblVacancies.VacancyPublished ELSE @VacancyPublished END)) AND 
                      (tblVacancies.UserID = (CASE WHEN @UserID IS NULL THEN tblVacancies.UserID ELSE @UserID END)) AND 
                      (tblVacancies.CountryID = (CASE WHEN @CountryID IS NULL THEN tblVacancies.CountryID ELSE @CountryID END)) AND 
                      (tblVacancies.VacancyTypeID = (CASE WHEN @VacancyTypeID IS NULL THEN tblVacancies.VacancyTypeID ELSE @VacancyTypeID END)) AND
					  (tblSectors.SectorID = (CASE WHEN @Sector IS NULL THEN tblSectors.SectorID ELSE @Sector END)) AND
					  (tblVacancies.SubSectorID = (CASE WHEN @SubSector IS NULL THEN tblVacancies.SubSectorID ELSE @SubSector END)) AND
					  (tblVacancies.Location = (CASE WHEN @Location IS NULL THEN tblVacancies.Location ELSE @Location END)) AND
					  (tblVacancies.RecruiterRef = (CASE WHEN @RecruiterRef IS NULL THEN tblVacancies.RecruiterRef ELSE @RecruiterRef END))
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros