Skip to content
Keep Learning Keep Living
Keep Learning Keep Living

For Loop Execute Immediate Enhancement – Small Bug

Mustafa Kalaycı, 2026-04-232026-05-05

Hi,

Oracle added a lot of For Loop enhancement on 21c. All supported on 23c and 26ai of course. One of them is dynamic sql (execute immediate) support in For Loop. I prefer for loop instead of “while loop” because it is more clear and mostly easier to read. here is an example:

Oracle PL/SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
create or replace package pkg_X as
  x number;
end;
/
 
Package PKG_X compiled
 
create or replace package body pkg_x as
  procedure sp_1 as
    type t_arr is table of varchar2(128) index by pls_integer;
    x_arr t_arr;
    type t_rec is record(con_id number, name varchar2(128));
  begin
    for r t_rec in (execute immediate 'select con_id, name from v$pdbs') loop
      x_arr(r.con_id) := r.name;
    end loop;
  end;
end;
/
 
Package Body PKG_X compiled

this is just an example. of course this code does not need dynamic sql but I just wanted to demonstrate the issue. So the package is compiled without an issue. By the way I am running this code on 26ai (23.26.0).

Problem starts when you add another procedure with a DATE column selection. (Interesting right?)

Oracle PL/SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
create or replace package body pkg_x as
  procedure sp_1 as
    type t_arr is table of varchar2(128) index by pls_integer;
    x_arr t_arr;
    type t_rec is record(con_id number, name varchar2(128));
  begin
    for r t_rec in (execute immediate 'select con_id, name from v$pdbs') loop
      x_arr(r.con_id) := r.name;
    end loop;
  end;
 
  procedure sp_2 as
    x_date date;
  begin
    select max(created) into x_date from user_objects;
  end;
end;
/
 
LINE/COL ERROR
--------- -------------------------------------------------------------
16/5 PLS-00801: internal error [*** ASSERT at file pdw4.c, line 1040; Cannot coerce from type 25 to type 12; PKG_X__TEST_USER__B__323913[16, 5]]

so we got an Internal error. It points ” PKG_X__TEST_USER__B__323913[16, 5]” line 16 which is “select max(created)…”. It is not related with table or column.

if you switch order of the procedures (put sp_2 above the sp_1) then it is compiled normally! Another interesting point is, it is happening when you select column like DATE, Timestamp etc but not on number or varchar2 column.

Oracle PL/SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
create or replace package body pkg_x as
  procedure sp_1 as
    type t_arr is table of varchar2(128) index by pls_integer;
    x_arr t_arr;
    type t_rec is record(con_id number, name varchar2(128));
  begin
    for r t_rec in (execute immediate 'select con_id, name from v$pdbs') loop
      x_arr(r.con_id) := r.name;
    end loop;
  end;
 
  procedure sp_2 as
    x_object_id number;
  begin
    select max(object_id) into x_object_id from user_objects;
  end;
end;
/
 
Package Body PKG_X compiled

so it is compiled again. as I said, varchar2 is also working. in any case there is a small compiler bug in here. I hope it will be fixed soon. I would like to use new features as much as possible.

wish you a healthy good life. stay positive in this era of AI…

21c 23ai 26ai Administration Development SQL / PLSQL

Post navigation

Previous post

Comments (2)

  1. Anthony Harper says:
    2026-04-23 at 23:31

    what happens if you used the dynamic cursor iteration control to instantiate the collection without the for loop? is compliation still sensitive to following procedures?

    begin
    x_arr := t_arr( for r t_rec in values of (execute immediate ‘select con_id, name from v$pdbs’)
    index r.con_id => r.name);
    end;

    Reply
    1. Mustafa Kalaycı says:
      2026-04-24 at 15:01

      Hi Anthony,
      Many thanks for sparing some time and adding a comment. I actually forgot about those 21c new constructors. After your comment I tried it :

      x_arr t_Arr := t_Arr(FOR r t_rec IN values OF (execute immediate ‘select con_id, name from v$pdbs’) INDEX r.con_id => r.name);

      but it says PLS-00865: This expression is not supported in default expressions.

      I thought maybe it is related with user defined type so I also tried this version:

      x_arr t_Arr := t_Arr(FOR con_id, name varchar2(120) IN pairs OF (execute immediate ‘select con_id, name from v$pdbs’) INDEX con_id => name);

      but it raise the same error too.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • For Loop Execute Immediate Enhancement – Small Bug
  • My New Nemesis APEX_JSON Package
  • Long Running Query by OCI Performance Hub
  • 26ai Alert Log Size
  • What Should Change After Base DB System Clone

Recent Comments

  1. Mustafa Kalaycı on For Loop Execute Immediate Enhancement – Small Bug
  2. Anthony Harper on For Loop Execute Immediate Enhancement – Small Bug
  3. Mustafa Kalaycı on 26ai Alert Log Size
  4. Fdo on 26ai Alert Log Size
  5. Mustafa Kalaycı on Old Obsolete Backup is not Deleted

Archives

  • April 2026
  • February 2026
  • January 2026
  • November 2025
  • August 2025
  • July 2025
  • June 2025
  • April 2025
  • November 2024
  • July 2024
  • April 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • October 2021
  • September 2021
  • August 2021
  • April 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • July 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • June 2018

Categories

  • 11g
  • 12c
  • 18c
  • 19c
  • 21c
  • 23ai
  • 26ai
  • Administration
  • Cloud
  • Compression
  • Development
  • Materialized View
  • Multi-tenant
  • Performance
  • Security
  • SQL / PLSQL
  • Uncategorized
  • Undocumented
  • Useful Scripts
©2026 Keep Learning Keep Living | WordPress Theme by SuperbThemes