-2

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?

Kaishu
  • 517
  • 1
  • 10
  • 18
  • You have a User-Defined Table Type (UDTT) input parameter (a.k.a. a TVP), and yet are passing in an `INT`. This usage will not work. Please see your other, related Question for several examples of how to accomplish a Median function / aggregate. – Solomon Rutzky Jan 26 '16 at 07:37

1 Answers1

4

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.

Daniel Hutmacher
  • 8,810
  • 1
  • 19
  • 51