1

I need a recursive scalar function in SQL Server 2014. My code is this:

CREATE FUNCTION Accounting_ToppestLevelID 
(
    @ID numeric(6,0)
)
RETURNS numeric(6,0)
AS
BEGIN
    declare @temp numeric(6,0)
    select @temp = a.ParentID from Accounting_AcntAccount a where a.ID = @ID
    if @temp is null
    begin
        return @ID
    end
    return Accounting_ToppestLevelID(@temp)
END

But after executing the code below error will appearance:

Msg 195, Level 15, State 10, Procedure Accounting_ToppestLevelID, Line 34
'Accounting_ToppestLevelID' is not a recognized built-in function name.

It is a logical error but how I can fix it?

3
  • @jarlh SQL Server 2014 Commented Sep 4, 2018 at 6:59
  • 1
    This is a performance killer. Commented Sep 4, 2018 at 7:19
  • @IvanStarostin Really? But there will not be big data in goal tables, I'll remind your advice for future projects :) Commented Sep 4, 2018 at 7:33

3 Answers 3

3

Try adding the schema prefix, ie.

return dbo.Accounting_ToppestLevelID(@temp)
Sign up to request clarification or add additional context in comments.

Comments

2

You should simply specify function's full name: it is implicitly created in dbo schema if no schema is specified, so your script should be:

CREATE FUNCTION Accounting_ToppestLevelID 
(
    @ID numeric(6,0)
)
RETURNS numeric(6,0)
AS
BEGIN
    declare @temp numeric(6,0)
    select @temp = a.ParentID from Accounting_AcntAccount a where a.ID = @ID
    if @temp is null
    begin
        return @ID
    end
    return dbo.Accounting_ToppestLevelID(@temp)
END

Comments

1

Try adding a schema explicitly. If you don't use a custom one, default is dbo:

return [dbo].[Accounting_ToppestLevelID](@temp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.