I’m trying to use IF else If logic inside an inline table valued function for SQL and returning a containstable based on that logic. but i’m having syntax problems with the IF Else IF block. thanks for the help. since i can’t parametrize the columns in the containstable i have to resort to using if else statements. here’s the code. thanks.
i’m getting
Msg 156, Level 15, State 1, Procedure FullTextSearch, Line 17 Incorrect syntax near the keyword ‘IF’.
ALTER FUNCTION [dbo].[FullTextSearch] ( @Columns nvarchar(100), @SearchPhrase nvarchar(100) ) RETURNS TABLE AS RETURN IF (@Columns='Title') BEGIN SELECT * from projects as P inner join containstable(PROJECTS, Title, @SearchPhrase) as K on P.project_id = K.[KEY] END ELSE IF (@Columns='Project_Details') BEGIN SELECT * from projects as P inner join containstable(PROJECTS, Project_Details, @SearchPhrase) as K on P.project_id = K.[KEY] END ELSE IF (@Columns='Contact_Info') BEGIN SELECT * from projects as P inner join containstable(PROJECTS, Contact_Info, @SearchPhrase) as K on P.project_id = K.[KEY] END ELSE IF (@Columns='Project_Description') BEGIN SELECT * from projects as P inner join containstable(PROJECTS, Project_Description, @SearchPhrase) as K on P.project_id = K.[KEY] END ELSE -- (@Columns='All') BEGIN SELECT * from projects as P inner join containstable(PROJECTS, (Title, Project_Details, Contact_Info, Project_Description), @SearchPhrase) as K on P.project_id = K.[KEY] END
You’re getting this error because you’re trying to use multiple statements in an inline table-valued function.
An inline table-valued function must contain a single
SELECTstatement, something along the lines of:Your function needs to be declared as a multi-statement function, syntax similar to this: