2010-11-19 2 views
1

Ich habe einige komplexe Auswahl und Ergebnis müssen in der Tabelle platzieren. Ich versuche dies:Orakel einfügen und komplex auswählen. Was ist los mit dir?

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); 

ich habe Fehler:

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 

Ob es diese Einschränkungen zu umgehen ist? Kann das Ergebnis von Select-Platz in Variable sein und dann Variable einfügen in Tabelle einfügen?

+0

, die für den SQL Server 2005+ Code Sie es von hob nicht einmal gültig ist. –

Antwort

2

WITH clause can't sepecified within parentheses.

Versuch Rewrite smth wie diese

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; 
+0

Nicht einmal in der Nähe ... –

+0

thx. toll arbeiten. – hexware

2
  1. keine Notwendigkeit für eine ORDER BY auf einer INSERT.

  2. ist ein Insert wie folgt codiert: INSERT INTO mytable (mycolumn, ...) SELECT ... so etwas wie dieses

Versuchen:

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 '____'; 
+0

In 10g: Sie können keinen query_name in einer eigenen Unterabfrage angeben. Jeder query_name, der in der Unterabfrage_factoring_clause definiert ist, kann jedoch in jedem nachfolgenden benannten Abfrageblock in der Unterabfrage_factoring_klause verwendet werden. (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#sthref9761) Obwohl ich denke, CTEs sind überbeansprucht und etwas wie Ihr Weg ist wahrscheinlich besser. –

+0

@jonearles: danke für die Korrektur :) –