views:

4870

answers:

3

Hi guys, 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
A: 

First of all, you have == on the first IF instead of =

Oh yeah and I noticed that within the CONTAINSTABLE parameters in your ELSE you have ProjectDetails listed twice

If you are searching a specific field could you just use this syntax:

WHERE Title LIKE '%'+ @Search +'%'
      OR ProjectDetails LIKE '%' + @Search + '%'
      OR ContactInfo LIKE '%' + @Search + '%')

(Warning) This syntax ignores any indexing.

You can check this blog for a solution that works with indexing. It seems to have a good solution.

jmein
sorry, i had that there for testing purposes, forgot to change it back when i was pasting the code. it's fixed to single "=" sorry about that, my SQL is novice at best.
stevenjmyu
LINQtoSQL is great because u can write context.contains(field, "search text") and it'll translate it to LIKE on the SQL side. the problem with that is Like isn't taking full advantage of full text search from SQL which is super fast if you have indexes for it. Using Like isn't an option for me.
stevenjmyu
i've implemented using LIKE but going though a table with millions of record on multiple fields takes a substantially longer time, compared to using full text search.
stevenjmyu
ah I see hmmm sounds like a tough one
jmein
did you notice that I mentioned that ProjectDetails is listed twice in your else
jmein
yea, sorry about that. was just in a hurry to test the IF ELSE statement. fixed
stevenjmyu
check out the blog post I just found it may help you out
jmein
haha yea that's where i got the idea to write a function and then calling it from LINQtoSQL queries. thanks for the help though.
stevenjmyu
A: 

Look at the multi-statement function template of the sql management studio i.e. on menu: View->Template Explorer-Create Multi-Statement Table-valued Function.

The issue is the syntax is different for the multi statement function. Also note that as jmein said there is a == on the first if.

eglasius
he fixed the = issue but I dont think it helped
jmein
Yes, because the real issue is the syntax for a multi-statement table-valued function is different
eglasius
thanks guys, i'm looking through the template for syntax right now.
stevenjmyu
+2  A: 

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 SELECT statement, something along the lines of:

CREATE FUNCTION dbo.MyInlineFunction (@MyParam INT)
RETURNS TABLE
AS
RETURN 
(
    SELECT first_column, second_column FROM ...
)

Your function needs to be declared as a multi-statement function, syntax similar to this:

CREATE FUNCTION dbo.MyMultiStatementFunction (@MyParam INT)
RETURNS @MyTable TABLE 
(
    first_column INT, second_column VARCHAR(100)  -- etc
)
AS
BEGIN
    IF (@MyParam = 1)
        INSERT INTO @MyTable (...)
        SELECT ... FROM ...
    ELSE
        INSERT INTO @MyTable (...)
        SELECT ... FROM ...

    RETURN
END
LukeH