1

以下更新语句是我第一次尝试在更新语句上使用内部联接。我的语法错误,但我不确定在哪里,但我不断收到命令未正确结束的错误。有人能帮忙吗。

UPDATE m_rqgrechunk a
SET    a.persondept = b.persondept
FROM   m_rqgrechunk a
       INNER JOIN m_person_persondept_hist b
         ON a.person = b.person
WHERE  ( ( b.from_date IS NULL
           AND b.to_date IS NULL )
          OR ( b.from_date IS NULL
               AND a.create_date < b.to_date )
          OR ( a.create_date >= b.from_date
               AND a.create_date < b.to_date )
          OR ( a.create_date >= b.from_date
               AND a.to_date IS NULL ) )
       AND a.persondept IS NULL 
4

2 回答 2

1

在 Oracle 中可以这样做:

UPDATE m_rqgrechunk a
SET    a.persondept = 
       ( SELECT b.persondept
         FROM m_person_persondept_hist b
         WHERE a.person = b.person
           AND ( ( b.from_date IS NULL
                   AND b.to_date IS NULL )
              OR ( b.from_date IS NULL
                   AND a.create_date < b.to_date )
              OR ( a.create_date >= b.from_date
                   AND a.create_date < b.to_date )
              OR ( a.create_date >= b.from_date
                   AND a.to_date IS NULL ) )
       )
WHERE a.persondept IS NULL 
  AND EXISTS 
       ( SELECT 1  
         FROM m_person_persondept_hist b 
         WHERE a.person = b.person  
           AND ( ( b.from_date IS NULL 
                   AND b.to_date IS NULL ) 
              OR ( b.from_date IS NULL  
                   AND a.create_date < b.to_date ) 
              OR ( a.create_date >= b.from_date 
                   AND a.create_date < b.to_date )
              OR ( a.create_date >= b.from_date
                   AND a.to_date IS NULL ) )
       ) ;

在这种情况下,这NOT EXISTS部分并不是真正需要的,因为要更新的值是这样的NULL,所以用 void 更新它们并没有什么坏处NULL,除了日志空间会无缘无故地膨胀。


另一种方法是使用派生表:

UPDATE 
( SELECT a.persondept, b.persondept AS persondept_new
  FROM m_rqgrechunk a
    JOIN m_person_persondept_hist b
      ON a.person = b.person
  WHERE ( ( b.from_date IS NULL
            AND b.to_date IS NULL )
       OR ( b.from_date IS NULL
            AND a.create_date < b.to_date )
       OR ( a.create_date >= b.from_date
            AND a.create_date < b.to_date )
       OR ( a.create_date >= b.from_date
            AND a.to_date IS NULL ) )
    AND a.persondept IS NULL
) 
SET persondept = persondept_new ;
于 2013-07-02T20:53:30.930 回答
0

恕我直言,加入更新是这样完成的

UPDATE m_rqgrechunk a
   INNER JOIN m_person_persondept_hist b
     ON a.person = b.person
SET    a.persondept = b.persondept
WHERE  ( ( b.from_date IS NULL
       AND b.to_date IS NULL )
      OR ( b.from_date IS NULL
           AND a.create_date < b.to_date )
      OR ( a.create_date >= b.from_date
           AND a.create_date < b.to_date )
      OR ( a.create_date >= b.from_date
           AND a.to_date IS NULL ) )
   AND a.persondept IS NULL 
于 2013-07-02T20:50:09.277 回答