SQL Server 中使用的函数的对象名称无效错误

Har*_*hna 1 sql-server

我正在使用 sql-server 2012:

drop table ttt
create table ttt(id int,amt int)
insert into ttt values(1,100),(2,200),(1,200),(3,400),(1,500)

create function dbo.t1(@id int)
returns int
with returns null on null input
as
begin
declare @amt_total int
select @amt_total=sum(amt) from ttt
where id=@id
return @amt_total
end

select t.id,t.amt,f.id from ttt t cross apply dbo.t1(t.id) f
Run Code Online (Sandbox Code Playgroud)

该代码在执行时返回以下错误: Msg 208, Level 16, State 3, Line 16 Invalid object name 'dbo.t1'.

有什么帮助吗?

函数、表都存在于同一模式中

Gio*_*sos 5

您使用的语法适用于表值用户定义函数,该函数返回表数据类型而不仅仅是标量值。

尝试使用以下语法:

SELECT t.id, t.amt, f.id 
FROM ttt t 
CROSS APPLY (SELECT dbo.t1(t.id) ) f(id)
Run Code Online (Sandbox Code Playgroud)