sql-server – 如何使用表值函数连接表?

我有一个用户定义的功能
create function ut_FooFunc(@fooID bigint,@anotherParam tinyint)
returns @tbl Table (Field1 int,Field2 varchar(100))
as
begin
  -- blah blah
end

现在我想在另一个表上加入这个,就像这样:

select f.ID,f.Desc,u.Field1,u.Field2
from Foo f 
join ut_FooFunc(f.ID,1) u -- doesn't work
where f.someCriterion = 1

换句话说,对于SomeCriterion为1的所有Foo记录,我想看到Foo ID和Desc,以及从ut_FooFunc返回的Foo.ID输入的Field1和Field2的值.

这样做的语法是什么?

解决方法

你需要交叉申请不加入.

连接中涉及的表表达式的定义必须是稳定的.即它们不能相关,因此表表达式意味着取决于另一个表中行的值的不同.

select f.ID,u.Field2
from Foo f 
Cross apply ut_FooFunc(f.ID,1) u
where f.someCriterion = ...

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...