在UDF

时间:2017-09-29 10:43:10

标签: sql sql-server user-defined-functions common-table-expression

有人知道如何在UDF中使用递归cte。我正在尝试与UDF实现我的家族树关系返回BIT。

代码如下:

DECLARE @UserID1 INT,
        @UserID2 INT;

SELECT  @UserID1 = 13,
        @UserID2 = 14;

with famMomRel as (
      select * 
      from dbo.people
      where id = @UserID1 
      union all
      select ft.*
      from dbo.people ft inner join
           famMomRel
           on ft.id = famMomRel.idMother    
     ),
     famDadRel as (
      select *   
      from dbo.people
      where id = @UserID1     
      union all
      select ft.*   
      from dbo.people ft inner join
           famDadRel
           on ft.id = famDadRel.idFather
     ), 
     SpouseRel as (
      select *   
      from dbo.people
      where id = @UserID1 
      union all
      select ft.*   
      from dbo.people ft inner join
           SpouseRel
           on ft.id = SpouseRel.idSpouse
     )
select *
from famMomRel
where  idMother = @UserID2   
union all
select *
from famDadRel
where  idFather = @UserID2
union all
select *
from famDadRel
where idSpouse = @UserID2 

如何将它实现到UDF中,返回位?

由于

0 个答案:

没有答案