加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 05:54:25 所属栏目:MsSql教程 来源:网络整理
导读:我有一个用户定义的功能: create function ut_FooFunc(@fooID bigint,@anotherParam tinyint)returns @tbl Table (Field1 int,Field2 varchar(100))asbegin -- blah blahend 现在我想在另一个表上加入这个,就像这样: select f.ID,f.Desc,u.Field1,u.Field2f
我有一个用户定义的功能:
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 = ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读