在 table 值函数中使用 select 查询作为参数

Using a select query as parameter in a table-valued function

我有一个函数接受一个整数输入和 returns 一个 table 与来自下面 table.

的相关 ID
ID  NAME    RELATED_ID
1   a       null
2   b       null
3   c       1
4   d       1

所以,select * 来自 fn_getrelatedids(1) returns

ID
1
3
4

我想使用动态参数作为函数的参数,即

select * from fn_getrelatedids (select e.id from entity e where e.name='a')

显然这行不通。

我试过交叉应用:

select e.id from entity e 
cross apply 
(select f.id from fn_getrelatedids (e.id) AS fg
WHERE fg.id = e.id) AS R
WHERE e.name = 'a'

这 return 不是正确的值。函数 return 仅

ID
1

我是不是漏掉了什么明显的东西?

看看这是否不是您想要做的。

select fg.id 
from entity e 
cross apply dbo.fn_getrelatedids(e.id) AS fg
WHERE e.name = 'a'

编辑:添加右边select

示例:

SELECT      table1.f_code, 
            table1.subsidiaryledger AS ledgerid, 
            table1.subgroupid , 
            table1.openingamt, 
            table1.ac_type, 
            0 AS debitamount, 
            0 AS creditamount, 
            closing , 
            closing_type 
FROM        ledgersubsidiaryopeningbalanace   AS table1 
** cross apply 
dbo.getsubsidaryclosingnew (table1.subsidiaryledger,table1.subgroupid,table1.openingamt,table1.ac_type,0,0) AS tv
WHERE       table1.subsidiaryledger = tv.ledgerid 
AND         table1.subgroupid = tv.subgroupid** 
AND         subsidiaryledger = 15105 
AND         f_code = 1