how to create and call scalar function in sql server 2008

Abbas picture Abbas · Dec 23, 2011 · Viewed 169.1k times · Source

I have created a Scalar Functions, it was created successfully, but when I call the function using select statement, it says invalid object, I altered the function, I got the message command completed successfully, but when I call the function, I gets same error. below is the function I am trying to call:

ALTER FUNCTION [dbo].[fn_HomePageSlider]
(
    @PortalID int,
    @ArticleID int
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @HTML NVARCHAR(MAX)
    SET @HTML = '';
    Declare @Title varchar(1000)
    Select @Title= Title from CrossArticle_Article c where c.Id=@ArticleID
    Select @HTML = @HTML + '<div class="homeSlider">
                                <div class="text">'+ISNULL(c.Title,'')+'</div>
                            </div>'
    FROM CrossArticle_Article c INNER JOIN crossarticle_url U ON U.articleid=c.Id
    INNER JOIN FREETEXTTABLE(CrossArticle_Article,TITLE,@TITLE) as INDEX_TBL 
    ON INDEX_TBL.[KEY]=c.Id
    WHERE INDEX_TBL.RANK >= 75 AND 
    c.Id<>@ArticleID AND
    c.PortalId=@PortalID
    GROUP BY c.Title,U.url,INDEX_TBL.RANK
    ORDER BY INDEX_TBL.RANK DESC

    RETURN @HTML;
END

And below is the way I am calling the function:

SELECT * FROM dbo.fn_HomePageSlider(9, 3025)

Can anyone tell me what's wrong with the above function, as I get the message command completed successfully.

Answer

Akhil picture Akhil · Dec 23, 2011

Your Call works if it were a Table Valued Function. Since its a scalar function, you need to call it like:

SELECT dbo.fn_HomePageSlider(9, 3025) AS MyResult