Oracle PL/SQL Select 使用 WITH 子句插入变量

Oracle PL/SQL Select into variable using WITH clause

我有一个 WITH 子句可以给我想要的结果,我正试图把它放在一个变量中。为了简单起见,我省略了代码。最后一行代码最相关。

WITH ALL_VE_ERRORS AS (

        SELECT *
        FROM ASN.AN_VALIDATION_ERRORS
        WHERE ...

    ), FILTER_STATUS AS (

        SELECT *
        FROM ALL_VE_ERRORS
        WHERE ...

      ) SELECT UNIT_DISCREPANCY FROM FILTER_STATUS INTO W_UNIT_DISCREPANCY; <-- like this

但这不行,编译器不喜欢这样。我也试过像这样把它放在第一位:

SELECT INTO W_UNIT_DISCREPANCY <-- or like this
       WITH ALL_VE_ERRORS AS (...

有人知道做这样的事情的正确语法吗?

如果你没有我看不到的理由,你的 WITH 子句中不需要两个表;你可以这样简化它:

WITH ALL_VE_ERRORS AS (    
    SELECT *
    FROM ASN.AN_VALIDATION_ERRORS
    WHERE ...    
)
SELECT UNIT_DISCREPANCY
INTO W_UNIT_DISCREPANCY
FROM ALL_VE_ERRORS
WHERE ...

否则,您可以使用:

WITH ALL_VE_ERRORS AS (    
        SELECT *
        FROM ASN.AN_VALIDATION_ERRORS
        WHERE ...    
    ), FILTER_STATUS AS (    
        SELECT *
        FROM ALL_VE_ERRORS
        WHERE ...    
      )
SELECT UNIT_DISCREPANCY
INTO W_UNIT_DISCREPANCY
FROM FILTER_STATUS