14

I am getting this error:

Msg 195, Level 15, State 10, Line 1
'fnParseName' is not a recognized built-in function name.

On this query:

SELECT  fnParseName(DOCTORFIRSTNAME+' ' +DOCTORLASTNAME) 
  FROM [PracticeandPhysician]

Here's the code for fnParseName

create FUNCTION [dbo].[fnParseName]
               (@FullName NVARCHAR(128))
RETURNS @FullNameParts TABLE  (FirstName  NVARCHAR(128),
                               Middle     NVARCHAR(128),
                               LastName   NVARCHAR(128))
AS
  BEGIN
    ... function body that populates @FullNameParts ...
    RETURN
  END

Why am I getting this error?

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

5 Answers5

20

It's a table-valued function. So you probably meant:

SELECT p.DOCTORFISTNAME, p.DOCTORLASTNAME, t.FirstName, t.Middle, t.LastName
  FROM dbo.[PracticeandPhysician] AS p
  CROSS APPLY dbo.fnParseName(p.DOCTORFIRSTNAME + ' ' + p.DOCTORLASTNAME);

Note that you can't say:

SELECT dbo.TableValueFunction('foo');

Any more than you could say:

SELECT dbo.Table;
--or
SELECT dbo.View;

You can, however, say:

SELECT * FROM dbo.fnParseName('foo bar');
--or
SELECT FirstName, Middle, LastName FROM dbo.fnParseName('foo bar');

(Not that I have validated that your function does what you think, or does so efficiently.)

Please always use the dbo. prefix as others have suggested.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Didn't I show that in the very first sample, using `CROSS APPLY`? It should output columns from every input row in `dbo.[PracticeandPhysician]`. – Aaron Bertrand Mar 06 '12 at 03:10
  • "CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function." From the MSDN documentation: http://msdn.microsoft.com/en-us/library/ms175156.aspx – Aaron Bertrand Mar 06 '12 at 04:04
  • plus one for 'always use the dbo prefix' – Hakan Fıstık Oct 24 '16 at 13:28
12

You always have to prefix SQL function calls with the schema name dbo. or the schema name for that function (dbo is the default schema).

SELECT dbo.fnParseName(--etc
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Peter Wishart
  • 11,600
  • 1
  • 26
  • 45
1

The problem you have is similar to what I encountered too. Scalar function and Table inline functions are quite different in terms of implementation. See below for the diiferent

 Create function udfCountry
 (
@CountryName varchar(50)
 )
 returns varchar(2)
 as 
 BEGIN
 Declare @CountryID varchar(2),
    @Result varchar(2)

 Select @CountryID = Country from 
dbo.GeoIPCountryNames where CountryName = @CountryName

set @Result = isNull(@CountryID, 'NA')
if @Result = 'NA'
set @Result = 'SD'
return @Result
End

//Implementation

select dbo.[udfCountry]('Nigeria')

// sample result

NG

// Inline table function sample

 Create FUNCTION  ConditionEvaluation
 (
  @CountrySearch varchar(50)
 )
 returns @CountryTable table 
(
Country varchar(2),
CountryName varchar(50)
 )
 as 

 Begin

 Insert into @CountryTable(Country, CountryName)
 Select Country, CountryName from GeoIPCountryNames
 where Country like '%'+@CountrySearch+'%'
 return 
 end

//Implementation sample

 Declare @CountrySearch varchar(50)
 set @CountrySearch='a'
 select * from  ConditionEvaluation(@CountrySearch)

the parttern of implementating scalar is quite different inline table. I hope this helps

user1656779
  • 241
  • 1
  • 2
  • 7
1

If you want to assign the value returned by tfn in a variable of stored procedure, you can do it this way:

select  @my_local_variable_in_procedure = column_name_returned_from_tfn from dbo.my_inline_tfn (@tfn_parameter)
Michael
  • 1,453
  • 3
  • 20
  • 28
Sandeep Gaadhe
  • 367
  • 4
  • 4
1

UDFs/Functions need to be prefixed with the schema name (most likely "dbo"). Change the call to

SELECT
    dbo.fnParseName(DOCTORFIRSTNAME + ' ' + DOCTORLASTNAME) 
FROM
    [PracticeandPhysician]
JackAce
  • 1,407
  • 15
  • 32
  • Msg 4121, Level 16, State 1, Line 2 Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnParseName", or the name is ambiguous. – Alex Gordon Mar 05 '12 at 23:23
  • The schema prefix is not the issue here (while it will still be required, there is a more fundamental syntax issue). – Aaron Bertrand Mar 05 '12 at 23:54