Use for loop after the With Clause in PL/SQL
Asked Answered
L

2

5

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

Livonia answered 30/5, 2016 at 4:47 Comment(1)
You'll want to become familiar with the differences between SQL and PL/SQL - they are two separate languages, although each can be embedded within the other.Wendolyn
B
9

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;
Betsybetta answered 30/5, 2016 at 7:30 Comment(0)
W
0

try creating a new table using this query. you can delete the table after you have done with your work.

Wessex answered 17/12, 2023 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.