0

我无法将此 SQL Server 代码转换为 Oracle 代码。我不是 SQL 专家,任何人都可以帮助将此代码转换为与 Oracle 兼容的代码。

 Declare @lvl as int
 Declare @rows as int
 DECLARE @foo as Table(
     KV_MANAGERNR int,
     KV_PERSONNR varchar(10),
     ord int,
     lvl int)

INSERT @foo (KV_MANAGERNR, KV_PERSONNR, ord, lvl)
select KV_MANAGERNR, KV_PERSONNR, row_number() over(order by KV_PERSONNR), 0
    from PERSONSMANAGER where KV_MANAGERNR='127723'

set @rows=@@ROWCOUNT
set @lvl=0

--Do recursion
WHILE @rows > 0
BEGIN
    set @lvl = @lvl + 1

    INSERT @foo (KV_MANAGERNR, KV_PERSONNR, ord, lvl)
    SELECT DISTINCT b.KV_MANAGERNR, b.KV_PERSONNR, row_number() over(order 
    by b.KV_PERSONNR), @lvl
    FROM PERSONSMANAGER b
     inner join @foo f on b.KV_MANAGERNR = f.KV_PERSONNR
     --might be multiple paths to this recursion so eliminate duplicates
     left join @foo dup on dup.KV_PERSONNR = b.KV_PERSONNR
     WHERE f.lvl = @lvl-1 and dup.KV_PERSONNR is null

    set @rows=@@ROWCOUNT 
END

SELECT DISTINCT KV_PERSONNR from @foo order by KV_PERSONNR
4

1 回答 1

0
DECLARE  
    v_lvl Number:=0;  
    v_rows number:=1;  
BEGIN  
    INSERT INTO nt_list_0 (KV_MANAGERNR, KV_PERSONNR, ord, lvl)  
        select KV_MANAGERNR, KV_PERSONNR, row_number() over(order by 
        KV_PERSONNR), 0  from PERSONSMANAGER where KV_MANAGERNR='100047';  

    WHILE v_rows>0  
    LOOP  
        v_lvl := v_lvl +1;  

        INSERT INTO nt_list_0 (KV_MANAGERNR, KV_PERSONNR, ord, lvl)  
        SELECT DISTINCT b.KV_MANAGERNR, b.KV_PERSONNR, row_number() 
          over(order by b.KV_PERSONNR), v_lvl  
        FROM PERSONSMANAGER b  
        inner join nt_list_0 f on b.KV_MANAGERNR = f.KV_PERSONNR  
        left join nt_list_0 dup on dup.KV_PERSONNR = b.KV_PERSONNR  
        WHERE f.lvl = v_lvl-1 and dup.KV_PERSONNR is null;   

        v_rows := sql%rowcount;  
    END LOOP;  
END;
于 2017-12-07T09:29:25.170 回答