我从 table 中删除,如果一列为空,我需要使用另一列

I delete from a table where if one column is null i need to use another column

DELETE FROM HERAPERM.SCRUBLITST
WHERE
    CASE 
       WHEN ODUDAT IS NOT NULL
          THEN ODUDAT > to_char(current_date - 2 month, 'MM')   
       ELSE ODCDAT > to_char(current_date - 2 month, 'MM')         
    END

这是获得您正在寻找的期望结果的一种方法。创建一个临时 table 来保存要删除的行的主键,然后在收集完所有行后将其从 table 中删除。

DECLARE @foundIDs TABLE ( ID INT )

INSERT INTO @foundIDs
SELECT ID FROM HERAPERM.SCRUBLITST
WHERE ODUDAT IS NULL
AND ODUDAT > to_char(current_date - 2 month, 'MM')

INSERT INTO @foundIDs
SELECT ID FROM HERAPERM.SCRUBLITST
WHERE ODUDAT IS NOT NULL
AND ODCDAT > to_char(current_date - 2 month, 'MM')

DELETE FROM HERAPERM.SCRUBLITST
WHERE ID IN (SELECT ID FROM @foundIDs)

在关于无法在 DB2 中声明临时 tables 的评论之后,这里有一个替代方法:

DELETE FROM HERAPERM.SCRUBLITST
WHERE ID IN (SELECT ID FROM HERAPERM.SCRUBLITST
    WHERE ODUDAT IS NULL
    AND ODUDAT > to_char(current_date - 2 month, 'MM'))
OR ID IN (SELECT ID FROM HERAPERM.SCRUBLITST
    WHERE ODUDAT IS NOT NULL
    AND ODCDAT > to_char(current_date - 2 month, 'MM'))

没有相关子查询和需要 ID 的更简单的解决方案:

DELETE FROM HERAPERM.SCRUBLITST
WHERE COALESCE (ODUDAT, ODCDAT) > to_char(current_date - 2 month, 'MM');