2012-03-08 98 views
1

如何使用腳本上的左連接更新表。我正在使用DB2數據庫。左連接更新表

我創建了一個SELECT語句,它的工作原理:

**

select t1.estrcd as "transaction code", 
     t1.espyno as "payer", 
     t1.escuno as "customer no", 
     t1.escino as "invoice no", 
     t1.esvono as "voucher no", 
     t1.escuam as "foreign currency amount", 
     COALESCE(t2."received_amount",0) as "received amount", 
     t1.escuam + COALESCE(t2."received_amount",0) as "outstanding amount" 
from m3edbedu.fsledg t1 left join 
     (select espyno, escino, sum(escuam) as "received_amount" from m3edbedu.fsledg 
       where estrcd = 20 group by espyno, escino) as t2 on 
     t2.espyno = t1.espyno and t2.escino = t1.escino 
where t1.esreco = 0 and t1.estrcd = 10 and (t1.escuam + COALESCE(t2."received_amount",0)) = 0 
order by t1.espyno, t1.escino, t1.estrcd; 

**

但現在他們要我更新表和t1.esreco設置爲9。我嘗試使用下面的腳本來更新,但我得到一個錯誤。

update m3edbedu.fsledg t1 LEFT JOIN(select espyno, escino, sum(escuam) as "received_amount" 
      from m3edbedu.fsledg 
      where estrcd = 20 
      group by espyno, escino) as t2 
      on t2.espyno = t1.espyno and t2.escino = t1.escino set t1.esreco = 9 where t1.esreco = 0 and t1.estrcd = 10 and (t1.escuam + COALESCE(t2."received_amount",0)) = 0 order by t1.espyno, t1.escino, t1.estrcd;1; 

Error: SQL0199 - Keyword LEFT not expected. Valid tokens: SET. (State:37000, Native Code: FFFFFF39) Error: SQL0104 - Token 1 was not valid. Valid tokens: (CL END GET SET CALL DROP FREE HOLD LOCK OPEN WITH ALTER BEGIN. (State:37000, Native Code: FFFFFF98)

我希望有人能幫助我。

在此先感謝。 :)

回答

1

由於這是一個DB2數據庫,因此不能在更新語句中指定FROM子句。更新答案以反映你必須做的事情(請記住,這絕不是最優化的)。

update 
    m3edbedu.fsledg t1 
set 
    t1.esreco = 9 
where 
    t1.esreco = 0 and t1.estrcd = 10 and 
    exists(select t2.espyno 
     from m3edbedu.fsledg t2 
     where t2.estrcd = 20 and t2.espyno = t1.espyno and t2.escino = t1.escino) 
    and (t1.escuam + coalesce(
     (select sum(t2.escuam) 
     from m3edbedu.fsledg t2 
     where t2.estrcd = 20 and t2.espyno = t1.espyno and t2.escino = t1.escino), 0) = 0) 
+0

感謝您的快速回復,但我仍然有同樣的錯誤:錯誤:SQL0199 - 關鍵字從不預期。有效令牌:在哪裏跳過。 (狀態:37000,本機代碼:FFFFFF39) 錯誤:SQL0104 - 令牌1無效。有效令牌:(CL END GET SET CALL DROP FREE HOLD LOCK WITH ALTER BEGIN。(狀態:37000,本機代碼:FFFFFF98) – Christian 2012-03-08 18:57:18

+0

應該已經讀過你正在使用的數據庫引擎,DB2顯然不喜歡使用FROM子句更新聲明,我會盡快給你一個更新的響應 – SPFiredrake 2012-03-08 19:04:59

0

試試這個:

update t1 
SET t1.esreco = 9 
from m3edbedu.fsledg t1 
LEFT JOIN (select espyno, escino, sum(escuam) as "received_amount" 
      from m3edbedu.fsledg 
      where estrcd = 20 
      group by espyno, escino) as t2 
on t2.espyno = t1.espyno 
and t2.escino = t1.escino 
where t1.esreco = 0 
and t1.estrcd = 10 
and (t1.escuam + COALESCE(t2."received_amount",0)) = 0 ; 
+0

hello ...謝謝你的回覆,但是錯誤仍然存​​在:錯誤:SQL0199 - 關鍵詞FROM not expected。有效令牌:SKIP WITH WHERE。(狀態:37000,Native代碼:FFFFFF39) – Christian 2012-03-08 19:00:34

0

DB2不支持聯接在UPDATE聲明,所以你必須做一個變通方法,比如做連接的一部分相關子查詢,因此:

UPDATE m3edbedu.fsledg update 
SET esreco = 9 
WHERE EXISTS (
    SELECT 1 
    FROM m3edbedu.fsledg t1 
    LEFT JOIN(
     SELECT espyno, 
       escino, 
       sum(escuam) as received_amount 
      FROM m3edbedu.fsledg 
     WHERE estrcd = 20 
     GROUP BY espyno, escino 
    ) as t2 
    ON t2.espyno = t1.espyno 
    AND t2.escino = t1.escino 
    WHERE t1.unique_key1 = update.unique_key1 
     AND t1.unique_key2 = update.unique_key2 
     AND t1.unique_key3 = update.unique_key3 
     --etc 

     AND t1.esreco = 0 
     AND t1.estrcd = 10 
     AND (t1.escuam + COALESCE(t2.received_amount,0)) = 0 
) 

你可能要檢查並確保你通過改變UPDATE部分爲正從子查詢右欄。

+0

非常感謝你的信息,我會嘗試你提到的那個,讓你知道會發生什麼,再次,謝謝。 – Christian 2012-03-09 13:52:06