在 PL/SQL 中的 With 子句之后使用 for 循环

Use for loop after the With Clause in PL/SQL

我正在使用 PL/SQL。在 with 子句中定义临时表后,我试图立即使用 for 循环。但是,我得到一个错误,首先要进行 SELECT 查询。

比如

WITH TMP1 AS (.....), TMP2 AS (......), TMP3 AS (......)

FOR R IN (SELECT DISTINCT ..... FROM TMP1 WHERE .....)
LOOP
SELECT .... FROM TMP2, TMP2 WHERE TMP2.... = R..... ....

我该怎么做?

谢谢

您无法在整个语句之外访问 CTE。并且您无法访问 CTE 的最终 SELECT 之外的各个部分。

您需要将整个 CTE(包括最后的SELECT语句)放入游标循环中:

FOR R IN (WITH TMP1 AS (.....), 
               TMP2 AS (......), 
               TMP3 AS (......)
          SELECT DISTINCT ..... 
          FROM TMP1 
             JOIN temp2 ON ... 
             JOIN temp3 ON ... 
          WHERE .....)
LOOP
   -- here goes the code that processes each row of the query
END LOOP;