有没有办法从函数中返回多个值?理想情况下希望沿着这些方向做某事(我知道语法是错误的!)
{{1}}
我根本没有使用SQL函数的经验,所以不确定是否可以这样做。
由于
答案 0 :(得分:2)
将inline table valued function
与cross apply
:
Create function fn_result(@val1 int, @val2 int, @val3 int)
returns table
as
Return (select @val1+@val2 as val12,
@val1+@val3 as val13, @val2+@val3 as val23)
go
select * from (select 1 as test1, 2 as test2, 3 as test3) t
cross apply dbo.fn_result(t.test1, t.test2, t.test3)
输出:
test1 test2 test3 val12 val13 val23
1 2 3 3 4 5