-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?

1
  • 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. Commented Jan 26, 2016 at 7:37

1 Answer 1

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.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.