Can I put a WHILE loop inside WHERE clause? I have a stored procedure where I'm trying to put in text searching capability. I have it working for an exact match like this:
AND (@exactString = ''
OR (CHARINDEX(@exactString, [Short Description]) > 0
OR CHARINDEX(@exactString, [Description]) > 0
OR CHARINDEX(@exactString, [Manufacturer]) > 0))
Next I'm trying to do a "any word" match and an "all words" match. I can get the search string I want to search for with the following WHILE loop:
DECLARE @searchString varchar(max)
DECLARE @endIndex int
SET @allString = LTRIM(RTRIM(@allString))
WHILE LEN(@allString) > 0
BEGIN
SET @endIndex = CHARINDEX(' ', @allString) > 0
IF @endIndex > 0
BEGIN
SET @searchString = LEFT(@allString, @endIndex)
SET @allString = LTRIM(RTRIM(RIGHT(@allString, @endIndex)))
END
ELSE
BEGIN
SET @searchString = @allString
SET @allString = ''
END
END
Now I want to use the @searchString variable like I used @exactString above. Is there a way to do this inside my loop or is there some other technique I'm missing that would work here?
Thanks for your help, Dan