在 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 ;