Syntax FOR <row> IN <cursor> LOOP cannot be used with dynamic SQL; see the example in the documentation , which shows the method you use when this code is commented out anyway.
Your example does not have to be dynamic, but I assume that you just simplified it for this question. If he had a placeholder, then there would be no way to establish its value. If you have:
LV_QUERY:='select sysdate - :days as mydate from dual'; FOR CUR_VAR IN LV_TEST_CUR LOOP dbms_output.put_line(CUR_VAR.mydate); END LOOP;
... then the version of FOR ... IN ... does not give you a place to assign a value to the days placeholder. You must use dynamic OPEN for this:
LV_QUERY:='select sysdate - :days as mydate from dual'; -- pass '1' as the bind variable OPEN LV_TEST_CUR FOR LV_QUERY USING 1; LOOP FETCH LV_TEST_CUR INTO LV_DATE; EXIT WHEN LV_TEST_CUR%NOTFOUND; DBMS_OUTPUT.PUT_LINE(LV_DATE); END LOOP; CLOSE LV_TEST_CUR;
Of course, you may not need a placeholder and just builds the query string dynamically, but the restriction still applies.
source share