how to create and call scalar function in sql server 2008

浪子不回头ぞ 提交于 2019-11-28 19:33:02

问题


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.


回答1:


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



回答2:


Try

SELECT dbo.function (parameters)



回答3:


Or you can simply use PRINT command instead of SELECT command. Try this,

PRINT dbo.fn_HomePageSlider(9, 3025)


来源:https://stackoverflow.com/questions/8619697/how-to-create-and-call-scalar-function-in-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!