oracle插入和复杂选择。怎么了?

时间:2010-11-19 02:59:44

标签: sql oracle oracle10g

我需要在表格中放置一些复杂的选择和结果。 我试试这个:

INSERT INTO SAMGUPS_STATISTIC_STANTION
(SAMGUPS_STATISTIC_STANTION_SEQ.nextval)
(
with PRG as (
select
 ID_OBJ
from PEREGON where ID_STAN1=&arrival
),
STN_OBJ as (
          select
          distinct ID_OBJ_P as ID_OBJ
          from TMO start with ID_OBJ=&dispatch
          connect by prior ID_OBJ_P=ID_OBJ
),
STAN as (
          select
          A_TOP.ID_POEZD
          ,A_TOP.vrsvop
          from STN_OBJ inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
          left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
          WHERE ATR.NOM_POEZD LIKE '____'
),
DATA_RESULT as 
(
          select
          /*count(*) over() as TotalCount*/
          to_char(&dispatch) as dispatch
          ,to_char(&arrival) as arrival
          ...
          ,ATR.PR_N_V_PZ
          from PRG inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and  A_TOP.KODOP_P in ('03','07')
                    left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
                    inner join STAN STN on STN.ID_POEZD = ATR.ID_POEZD  
                    WHERE ATR.NOM_POEZD LIKE '____'
                    order by A_TOP.ID_POEZD
)
SELECT * FROM DATA_RESULT);

我有错误:

Error at Command Line:71 Column:25
Error report:
SQL Error: ORA-32034: unsupported use of WITH clause
32034. 00000 -  "unsupported use of WITH clause"
*Cause:    Inproper use of WITH clause because one of the following two reasons
           1. nesting of WITH clause within WITH clause not supported yet
           2. For a set query, WITH clause can't be specified for a branch.
           3. WITH clause can't sepecified within parentheses.
*Action:   correct query and retry

是否有规避这些限制? 可能是选择放入变量然后使用变量插入表的结果吗?

2 个答案:

答案 0 :(得分:2)

  

WITH子句不能在括号内指定。

尝试像这样重写smth

INSERT INTO SAMGUPS_STATISTIC_STANTION
with PRG as (
select
 ID_OBJ
from PEREGON where ID_STAN1=&arrival
),
STN_OBJ as (
          select
          distinct ID_OBJ_P as ID_OBJ
          from TMO start with ID_OBJ=&dispatch
          connect by prior ID_OBJ_P=ID_OBJ
),
STAN as (
          select
          A_TOP.ID_POEZD
          ,A_TOP.vrsvop
          from STN_OBJ inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
          left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
          WHERE ATR.NOM_POEZD LIKE '____'
),
DATA_RESULT as 
(
          select
          /*count(*) over() as TotalCount*/
          to_char(&dispatch) as dispatch
          ,to_char(&arrival) as arrival
          ...
          ,ATR.PR_N_V_PZ
          from PRG inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and  A_TOP.KODOP_P in ('03','07')
                    left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
                    inner join STAN STN on STN.ID_POEZD = ATR.ID_POEZD  
                    WHERE ATR.NOM_POEZD LIKE '____'
                    order by A_TOP.ID_POEZD
)
SELECT SAMGUPS_STATISTIC_STANTION_SEQ.nextval,  DATA_RESULT.* FROM DATA_RESULT;

答案 1 :(得分:2)

  1. INSERT上不需要ORDER BY。

  2. INSERT的编码如下:INSERT INTO mytable (mycolumn, ...) SELECT ...

  3. 尝试这样的事情:

    INSERT INTO SAMGUPS_STATISTIC_STANTION
    (dispatch, arrival, ..., PR_N_V_PZ)
              select
              to_char(&dispatch) as dispatch
              ,to_char(&arrival) as arrival
              ...
              ,ATR.PR_N_V_PZ
              from (select
                     ID_OBJ
                    from PEREGON where ID_STAN1=&arrival
                    )
              inner join A_TOP on A_TOP.ID_OBJ=PRG.ID_OBJ and  A_TOP.KODOP_P in ('03','07')
                        left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
                        inner join (select
                                    A_TOP.ID_POEZD
                                    ,A_TOP.vrsvop
                                    from (select
                                          distinct ID_OBJ_P as ID_OBJ
                                          from TMO start with ID_OBJ=&dispatch
                                          connect by prior ID_OBJ_P=ID_OBJ
                                    ) inner join A_TOP on A_TOP.ID_OBJ=STN_OBJ.ID_OBJ and A_TOP.KODOP_P in ('01','07')
                                    left join A_POEZD_ATR ATR on ATR.ID_POEZD=A_TOP.ID_POEZD and ATR.VRSVOP=A_TOP.VRSVOP
                                    WHERE ATR.NOM_POEZD LIKE '____') STN
                                on STN.ID_POEZD = ATR.ID_POEZD  
                        WHERE ATR.NOM_POEZD LIKE '____';