How to fix "I can’t find either the dbo column, the user-defined function or aggregate, or the name is ambiguous"

I need to call a function on a SQL server, but get an error!

 cannot find either column "dbo" or the user-defined function or
    aggregate "dbo.udf_Sum_ExtraHours", or the name is ambiguous.

I have a function that I received yesterday from the stack, and it works fine when I tested it in the management studio separately, but when I put it in the built-in function and you needed to call the stored procedure, then it throws the mentioned error.

which I saved:

ALTER FUNCTION dbo.udf_Sum_ExtraHours
    (
    @strt date,
    @end date

    )
RETURNS  TABLE 
AS
    RETURN

    WITH cte
AS (
    SELECT ExtraHrs 
        ,CASE 
            WHEN left(ExtraHrs, 1) = '-'
                THEN - 1
            ELSE 1
            END AS multiply
        ,right(ExtraHrs, 8) AS timestring
        ,
        --get hours in seconds:
        DATEPART(HOUR, right(ExtraHrs, 8)) * 3600 AS h_in_s
        ,
        --get minutes in seconds:
        DATEPART(MINUTE, right(ExtraHrs, 8)) * 60 AS m_in_s
        ,
        --get seconds:
        DATEPART(SECOND, right(ExtraHrs, 8)) AS s
    FROM  vw_Rept_Attend  where convert(date,AtnDate) between @strt and @end 
    )
    ,CTE3
AS (
    SELECT *
        ,c.h_in_s + c.m_in_s + c.s AddExtra
    FROM cte c
    )
    ,cte4
AS (
    SELECT sum(AddExtra * multiply) mn
    FROM cte3
    )
    ,cte5
AS (
    SELECT mn / 3600 hh
        ,(mn % 3600) / 60 mi
        ,(mn % 3600.0) % 60 ss
    FROM cte4
    )
SELECT 
    cast(hh AS VARCHAR) + ':' + cast(mi AS VARCHAR) + ':' + cast(ss AS VARCHAR) as ExtraHrs
FROM cte5

now the stored procedure where I want to call this function from

     select   UserID,
dbo.udfTimeSpanFromSeconds(Sum(Left(workhrs,2) * 3600 + substring(Convert(varchar(8),workhrs), 4,2) * 60 + substring(Convert(varchar(8),workhrs), 7,2))) as WorkHrs ,

dbo.udf_Sum_ExtraHours('2015-10-12','2015-10-14'),// function which throw error

EmpName,EmpType,UserName, Role,convert(VARCHAR(10),
StartDate,105) as StartDate,convert(VARCHAR(10),EndDate,105) as EndDate
from    vw_Rept_Attend  where  convert(date,AtnDate) between '2015-10-12' and '2015-10-14' 
group by UserID,

EmpName,EmpType,UserName, Role,StartDate,EndDate
      Order by UserID

but in the studio function of SQL server management, give me the exact result when I execute a separate function without using a stored procedure

out is placed in an SQL management studio that looks like:

enter image description here

I read

"dbo " " dbo.Splitfn"

"dbo" , , "dbo.FN_Split" ,

, ,

, ,

+4
1

select.

(, ;)):

  • :

    select [...], extraTime, [...]
    from    vw_Rept_Attend 
    cross apply dbo.udf_Sum_ExtraHours('2015-10-12','2015-10-14') as ex(extraTime)
    [...]
    
  • :

    select [...], 
    (    
        select top 1 ExtraHrs from dbo.udf_Sum_ExtraHours('2015-10-12','2015-10-14')
    ) ExtraHrs, [...]
    from    vw_Rept_Attend 
    [...]
    
  • , :

    ALTER FUNCTION dbo.udf_Sum_ExtraHours
    (
    @strt date,
    @end date
    
    )
    RETURNS INT -- or VARCHAR or some other single value type
    /* method body returning single value */
    
+3

Source: https://habr.com/ru/post/1611291/


All Articles