Oracle CTE查询:数据类型错误ORA-00932

时间:2012-12-04 18:55:57

标签: sql oracle oracle11g recursive-query

我正在Oralce 11g中尝试以下CTE查询,但我收到错误说

  

ORA-00932:数据类型不一致:预期NUMBER获得CHAR。

任何想法是什么原因造成的?

create table PC (
EMP_ID NUMBER NULL,
MGR_ID NUMBER NULL
);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (1.0, NULL);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (2.0, 1.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (3.0, 1.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (4.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (5.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (6.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (7.0, 3.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (8.0, 5.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (9.0, 7.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (10.0, 5.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (11.0, 7.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (12.0, 9.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (13.0, 9.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (14.0, 9.0);

SELECT * FROM pc;

with  Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as
        (
        SELECT  EMP_ID
        ,       MGR_ID
        ,       1 as lv
        ,       1 as level1
        ,       null as level2
        ,       null as level3
        ,       null as level4
        ,       null as level5
        FROM    PC
        WHERE   MGR_ID IS NULL
        UNION ALL
        SELECT  E.EMP_ID
        ,       E.MGR_ID
        ,       T.lv + 1
        ,       case when T.lv + 1 = 1 then E.EMP_ID else T.level1 end
        ,       case when T.lv + 1 = 2 then E.EMP_ID else T.level2 end
        ,       case when T.lv + 1 = 3 then E.EMP_ID else T.level3 end
        ,       case when T.lv + 1 = 4 then E.EMP_ID else T.level4 end
        ,       case when T.lv + 1 = 5 then E.EMP_ID else T.level5 end
        FROM    Tree T inner join PC E
                ON      (T.EMP_ID = E.MGR_ID)
        )
select  *
from    Tree

1 个答案:

答案 0 :(得分:4)

如果您将查询的第一部分更改为使用零作为level1等的值而不是null,则查询将起作用:

with  Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as
        (
        SELECT  EMP_ID
        ,       MGR_ID
        ,       1 as lv
        ,       1 as level1
        ,       0 as level2
        ,       0 as level3
        ,       0 as level4
        ,       0 as level5
        FROM    PC
        WHERE   MGR_ID IS NULL
        UNION ALL
        SELECT  E.EMP_ID
        ,       E.MGR_ID
        ,       T.lv + 1
        ,       case when T.lv + 1 = 1 then E.EMP_ID else T.level1 end
        ,       case when T.lv + 1 = 2 then E.EMP_ID else T.level2 end
        ,       case when T.lv + 1 = 3 then E.EMP_ID else T.level3 end
        ,       case when T.lv + 1 = 4 then E.EMP_ID else T.level4 end
        ,       case when T.lv + 1 = 5 then E.EMP_ID else T.level5 end
        FROM    Tree T inner join PC E
                ON      (T.EMP_ID = E.MGR_ID)
        )
select  *
from    Tree

请参阅SQL Fiddle with Demo

结果:

| EMP_ID | MGR_ID | LV | LEVEL1 | LEVEL2 | LEVEL3 | LEVEL4 | LEVEL5 |
---------------------------------------------------------------------
|      1 | (null) |  1 |      1 |      0 |      0 |      0 |      0 |
|      2 |      1 |  2 |      1 |      2 |      0 |      0 |      0 |
|      3 |      1 |  2 |      1 |      3 |      0 |      0 |      0 |
|      4 |      2 |  3 |      1 |      2 |      4 |      0 |      0 |
|      5 |      2 |  3 |      1 |      2 |      5 |      0 |      0 |
|      6 |      2 |  3 |      1 |      2 |      6 |      0 |      0 |
|      7 |      3 |  3 |      1 |      3 |      7 |      0 |      0 |
|      8 |      5 |  4 |      1 |      2 |      5 |      8 |      0 |
|      9 |      7 |  4 |      1 |      3 |      7 |      9 |      0 |
|     10 |      5 |  4 |      1 |      2 |      5 |     10 |      0 |
|     11 |      7 |  4 |      1 |      3 |      7 |     11 |      0 |
|     12 |      9 |  5 |      1 |      3 |      7 |      9 |     12 |
|     13 |      9 |  5 |      1 |      3 |      7 |      9 |     13 |
|     14 |      9 |  5 |      1 |      3 |      7 |      9 |     14 |

或者您可以cast(null as number)并且查询可以正常运行:

with  Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as
        (
        SELECT  EMP_ID
        ,       MGR_ID
        ,       1 as lv
        ,       1 as level1
        ,       cast(null as number) as level2
        ,       cast(null as number) as level3
        ,       cast(null as number) as level4
        ,       cast(null as number) as level5
        FROM    PC
        WHERE   MGR_ID IS NULL
        UNION ALL
        SELECT  E.EMP_ID
        ,       E.MGR_ID
        ,       T.lv + 1
        ,       case when T.lv + 1 = 1 then E.EMP_ID else T.level1 end
        ,       case when T.lv + 1 = 2 then E.EMP_ID else T.level2 end
        ,       case when T.lv + 1 = 3 then E.EMP_ID else T.level3 end
        ,       case when T.lv + 1 = 4 then E.EMP_ID else T.level4 end
        ,       case when T.lv + 1 = 5 then E.EMP_ID else T.level5 end
        FROM    Tree T inner join PC E
                ON      (T.EMP_ID = E.MGR_ID)
        )
select  *
from    Tree

请参阅SQL Fiddle with Demo