Se eu tiver um termo de pesquisa que seja igual a "Olá Connor Mcgregor", como eu usaria esse termo de pesquisa para que ele repetisse cada palavra de pesquisa na lista, por exemplo:
searchTerm[0] = "Hello";
searchTerm[1] = "Conner"
searchTerm[2] = "Mcgregor"
e pesquisa cada valor separado por um espaço com um campo ou com dois campos na tabela e retorna essa linha.
Nos últimos anos, tenho usado o EntityFramework, mas tenho tentado isso no LINQ e descobri que é melhor usar o ADO.NET e um procedimento armazenado.
O que foi tentado:
DECLARE @pos INT
DECLARE @len INT
DECLARE @value nvarchar(80)
DECLARE @sql nvarchar(max)
set @pos = 0
set @len = 0
WHILE CHARINDEX(' ', @searchTerm, @pos+1) > 0
BEGIN
set @len = CHARINDEX(',', @searchTerm, @pos+1) - @pos
set @value = SUBSTRING(@searchTerm, @pos, @len)
if exists (select title, questionText from tblThread where title like @value + '%')
BEGIN
set @sql = (select title, questionText from tblThread where title like @value + '%')
Print @sql
--DO YOUR MAGIC HERE
END
set @pos = CHARINDEX(',', @searchTerm, @pos+@len) +1
END
Eu tentei isso, mas não consigo adicionar os valores na tabela temporária
ALTER PROCEDURE [dbo].[getThreadsBySearchTerm]
@searchTerm nvarchar(80)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @searchTermTbl TABLE (searchTerm nvarchar(max))
DECLARE @value nvarchar(max)
DECLARE @pos INT
DECLARE @len INT
DECLARE @sql nvarchar(max)
DECLARE @sqlWithSpace Int
set @pos = 0
set @len = 0
while CHARINDEX(' ', @searchTerm, @pos+1) > 0
Begin
set @len = CHARINDEX(' ', @searchTerm, @pos+1) - @pos
set @value = SUBSTRING(@searchTerm, @pos, @len)
insert into @searchTermTbl values(@value)
set @pos = CHARINDEX(' ', @searchTerm, @pos+@len) +1
End
if exists(select * from @searchTermTbl)
begin
select title, questionText from tblThread
inner join @searchTermTbl stt on tblThread.title Like '%' + stt.searchTerm + '%'
inner join @searchTermTbl sttt on tblThread.questionText Like '%' + sttt.searchTerm + '%'
end
else
begin
select title, questionText from tblThread
where title like '%' + @searchTerm + '%'
or questionText like '%' + @searchTerm + '%'
end
select * from @searchTermTbl
END
GO
exec getThreadsBySearchTerm 'Question is'
está adicionando "Pergunta", mas não "é"