Use for loop after the With Clause in PL/SQL

泄露秘密 提交于 2021-02-08 15:46:05

问题


Im using PL/SQL. I am trying to have a for loop right after I define my temporary tables in the with clause. However, Im getting an error to have a SELECT query first.

For instance

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

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

How do I go about doing so?

Thanks


回答1:


You can't access a CTE outside of the whole statement. And you can't access individual parts of a CTE outside of the final SELECT for a CTE.

You need to put the whole CTE (including the final SELECT statement) into the cursor loop:

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;


来源:https://stackoverflow.com/questions/37517772/use-for-loop-after-the-with-clause-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!