I have been staring at this for too long and I can’t seem to see where I went wrong.
I have a stored procedure that has to return a bunch of data where the "ExtractedText" matches the word the person is searching for:
Select @Command = 'select DISTINCT CaseFileEvents.InvestigatorID,convert(nvarchar,EventDate,111) as ''EventDate'',EventTime,EventDesc,TaskID,Privileged,Private,Email,HasAttachments,FName,LName, FName + '' '' + LName as Name ,CaseFileEvents.FileID,CaseFiles.FileName,ItemEntryGradeID, EventDescPlainText
from CaseFileEvents
join ......
WHERE '+ @FilterField +' LIKE ''%' + @FilterQuery + '%'' ORDER BY ' + @SortName + ' ' + @SortOrder + ''; this area seems to bug out
@FilterField is a column in one of the tables, @FilterQuery is the word the user typed in that it is looking for. @SortName, is the name by wich it gets sorted.
Command examples: @FilterField = "ExtractedText", @FilterQuery="something", @SortName="EventID", @SortOrder="desc"
This is the error:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword ‘ORDER’.
Full command:
WHERE ExtractedText LIKE '%add%' ORDER BY EventID desc;
The reason why it didn’t work is because I was missing
WHERE EventID = convert(nvarcahr,@EventID)and just took out “DISTINCT”