2013-06-03 108 views
0
UPDATE Table1 a 
SET a.Col2 = (
    SELECT SubStr(Trim(b.Col2),-10,10) 
    FROM Table2 b 
    WHERE (InStr(SubStr(Trim(b.Col2),-5,5),'/')=1 
     AND InStr(SubStr(Trim(b.Col2),1,(Length(Trim(b.Col2))-10)),'/')=0 
     AND SubStr(b.Col1,1,2)='01' 
     AND b.Col3 != 000103)) 
WHERE SubStr(b.Col1,-8,8)=SubStr(a.Col1,1,8) 
    AND a.Col3 = 1; 

錯誤在命令行:9欄:14 錯誤報告: SQL錯誤:ORA-00904: 「B」 「Col1中」:無效的標識符 00904. 00000 - 「%S:無效標識符」更新查詢標識符

9號線是WHERE SubStr(b.Col1,-8,8)=SubStr(a.Col1,1,8)

+0

你引用內部查詢在外面。不要將這個條件移入內部查詢。 – Ben

+0

您在SET a.birthdate =(...)中使用的子查詢不能有任何外部引用。 – Sebas

+0

如果我無法引用它,我該如何加入這兩個表? –

回答

1

你可以嘗試MERGE

MERGE INTO Table1 a 
USING (
    SELECT * 
    FROM Table2 b 
    WHERE (InStr(SubStr(Trim(b.Col2),-5,5),'/')=1 
     AND InStr(SubStr(Trim(b.Col2),1,(Length(Trim(b.Col2))-10)),'/')=0 
     AND SubStr(b.Col1,1,2)='01' 
     AND b.Col3 != 000103) 
) c ON (SubStr(c.Col1,-8,8)=SubStr(a.Col1,1,8)) 
WHEN MATCHED THEN 
    UPDATE SET a.Col2 = SubStr(Trim(c.Col2),-10,10) 
     WHERE a.Col3 = 1;