2012-03-08 56 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