why unable to execute table-valued function?

Posted on

Question :

Here is table-value function I have declared like:

CREATE FUNCTION fn_GetMedian(@List TypeMedian READONLY)
RETURNS INT
AS
BEGIN
    RETURN 
    (
        Select 
        (
            (
                Select Top 1 Value
                From   
                (
                    Select  Top 50 Percent Value
                    From    @List
                    Where   Value Is NOT NULL
                    Order By Value
                ) As A
                Order By Value DESC
            ) + 
            (
                Select Top 1 Value
                From   
                (
                    Select  Top 50 Percent Value
                    From    @List
                    Where   Value Is NOT NULL
                    Order By Value DESC
                ) As A
                Order By Value Asc
            )
        ) / 2
    )
END

Here is code temp table and query I’m executing:

DECLARE @Temp Table
(ID INT,ID2 INT)

select fn_GetMedian(ID)
from @Temp

But, I’m getting error as:

Msg 195, Level 15, State 10, Line 9
‘fn_GetMedian’ is not a recognized function name.


This question is related to:

How can I pass column to function in sql?

Answer :

Functions need to be prefixed with the schema, to separate them from built-in functions.

The correct syntax should read

DECLARE @Temp Table
(ID INT,ID2 INT)

select dbo.fn_GetMedian(ID)
from @Temp

.. provided that your function is in the dbo schema.

Leave a Reply

Your email address will not be published. Required fields are marked *