Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create and call scalar function in sql server 2008

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.

like image 708
Abbas Avatar asked Dec 23 '11 18:12

Abbas


People also ask

How do you call a scalar value function in SQL?

Scalar-valued functions can be executed by using the EXECUTE statement. If you EXECUTE a function rather than use it in a SELECT statement or constraint, you can leave out the schema name in the function name, and it will look in the dbo schema followed by the users default schema.


2 Answers

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 
like image 117
Akhil Avatar answered Oct 06 '22 05:10

Akhil


Try

SELECT dbo.function (parameters) 
like image 28
Vijay Singh Rana Avatar answered Oct 06 '22 05:10

Vijay Singh Rana