oracle sql错误案例当其他时候

时间:2015-06-22 06:26:47

标签: sql oracle oracle11g

SELECT * 
FROM   FirstTable
WHERE  RowProcessed = 'N'
AND    (
        CASE
            WHEN EXISTS(SELECT top 1 FROM SecondTable)
            THEN 1
            ELSE EXISTS(
                         SELECT SecondTable.RowProcessed 
                         FROM   SecondTable 
                         WHERE  FirstTable.Key = SecondTable.Key
                         AND    SecondTable.RowProcessed = 'Y'
                       )
        END
       )
AND OtherConditions

案例在where子句中的其他地方。不确定语法。

我想验证SecondTable中是否有行,如果有行检查另一个条件,那基本上就是我想要做的。

1 个答案:

答案 0 :(得分:0)

也许这就是你想要的?如果SecondTable中有行,则执行第二个EXISTS

SELECT * FROM FirstTable
WHERE RowProcessed = 'N'
AND (NOT EXISTS (SELECT 1 from SecondTable)
      OR EXISTS (SELECT 1 FROM SecondTable
                 WHERE FirstTable.Key = SecondTable.Key
                   and SecondTable.RowProcessed = 'Y'))
AND OtherConditions