2014-04-08 1 views
0

작동하지 않습니다에서오라클 SQL 상관 된 하위 쿼리 내 다음 문 뭐가 잘못

ORA-00904: "TableToUpdate"."Date": Ungültiger Bezeichner 

: 내가 오라클에서 오류가 발생합니다 이것의 실행에

UPDATE TableToUpdate SET ColumnToUpdate = (
    SELECT ColumnWithNewValues 
    FROM (
     SELECT ColumnWithNewValues, ROWNUM AS N 
     FROM Table1 t1, Table2 t2  -- join tables 
     WHERE t2.Schluessel = t1.Schluessel -- join condition 
     AND t1.DateFrom <= TableToUpdate.Date -- <==== Error, reference to TableToUpdate 
     AND t1.DatumTo >= TableToUpdate.Date 
     -- ... some other conditions, not important here ... 
    ) tmp 
    WHERE tmp.N = 5   -- Use the fifth row to update the row of TableToUpdate 
) 

영어이게 무슨 뜻인지 알 것 같아.

ORA-00904: "TableToUpdate"."Date": Invalid identifier 

그래서 내가 상관 subquer에서 TableToUpdate를 참조 할 수없는 것 같습니다 SELECT 문에서 y. MSSQL에서는 oracle 특정 ROWNUM을 으로 바꾸는 대신 동일한 기술을 사용합니다.

나를 도와 줄 사람이 있습니까?

답변

0

하위 수준의 두 단계에서 가장 바깥 쪽 테이블을 참조하고 있습니다. 제한은 한 레벨을 위로 만 참조 할 수 있다는 것입니다. 따라서 오류 메시지.

병합 성명에 업데이트 문을 다시 작성하면이 제한을 피할 수 있습니다. 예를 들어, 다음과 같이 테스트되지 않았습니다.

merge into tabletoupdate t 
using (select datefrom 
      , datumto 
      , ColumnWithNewValues 
      from (select t1.datefrom 
         , t1.datumto 
         , ColumnWithNewValues 
         , rownum as n 
        from table1 t1 
         inner join table2 t2 on (t2.Schluessel = t1.Schluessel) -- join condition 
       --where ... some other conditions, not important here ... 
       --order by ... some columns here, otherwise rownum is meaningless 
       ) tmp 
     where tmp.n =5   -- Use the fifth row to update the row of TableToUpdate 
    ) 
    on ( t1.DateFrom <= t.Date 
     and t1.DatumTo >= t.Date 
    ) 
when matched then 
     update set t.columntoupdate = tmp.columnwithnewvalues