why unable to execute table-valued function?

All we need is an easy explanation of the problem, so here it is.

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?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply