Qualified Expressions Enhancements in Oracle Database 21c
Oracle database 21c makes it even easier to populate collections using qualified expressions.
oracle 21cconfigurationintermediate
by OracleDba
13 views
Oracle database 21c makes it even easier to populate collections using qualified expressions.
1234567891011121314151617181920212223242526272829303132
create or replace function num_to_word (p_num number) return varchar2 is
begin
return upper(to_char(to_date(p_num, 'j'), 'jsp'));
end;
/
-- drop table t1 purge;
create table t1 as
select level+10 as id,
num_to_word(level+10) as description,
trunc(sysdate) as created_date
from dual
connect by level <= 5;
column description format a20
column created_date format a12
select id, description, created_date from t1;
ID DESCRIPTION CREATED_DATE
---------- -------------------- ------------
11 ELEVEN 02-JAN-21
12 TWELVE 02-JAN-21
13 THIRTEEN 02-JAN-21
14 FOURTEEN 02-JAN-21
15 FIFTEEN 02-JAN-21
SQL>
SQL> set serveroutput on12345678910111213141516171819
declare
type t_tab is table of varchar2(10) index by pls_integer;
l_tab t_tab;
begin
-- Pre-18c - Direct assignment to elements of the collection.
l_tab(1) := 'ONE';
l_tab(2) := 'TWO';
l_tab(3) := 'THREE';
-- 18c - Qualified expression using named association.
l_tab := t_tab(1 => 'ONE',
2 => 'TWO',
3 => 'THREE');
-- 21c - Qualified expression using positional notation.
l_tab := t_tab('ONE', 'TWO', 'THREE');
end;
/1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
declare
type tab_t is table of pls_integer index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 => i+10
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=11
index=2 value=12
index=3 value=13
index=4 value=14
index=5 value=15
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50) index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=ONE
index=2 value=TWO
index=3 value=THREE
index=4 value=FOUR
index=5 value=FIVE
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50) index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 50 by 2 while i < 20 when i > 10 => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=11 value=ELEVEN
index=13 value=THIRTEEN
index=15 value=FIFTEEN
index=17 value=SEVENTEEN
index=19 value=NINETEEN
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
declare
type tab_t is table of pls_integer index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 index i*10 => i*100
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=10 value=100
index=20 value=200
index=30 value=300
index=40 value=400
index=50 value=500
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of date index by varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 index num_to_word(i) => sysdate+i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || rpad(i,5,' ') || ' value=' || v);
end loop;
end;
/
index=FIVE value=07-JAN-21
index=FOUR value=06-JAN-21
index=ONE value=03-JAN-21
index=THREE value=05-JAN-21
index=TWO value=04-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of date index by varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 50 by 2 while i < 20 when i > 10 index num_to_word(i) => sysdate+i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || rpad(i,9,' ') || ' value=' || v);
end loop;
end;
/
index=ELEVEN value=13-JAN-21
index=FIFTEEN value=17-JAN-21
index=NINETEEN value=21-JAN-21
index=SEVENTEEN value=19-JAN-21
index=THIRTEEN value=15-JAN-21
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
declare
type tab_t is table of pls_integer index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 11 .. 15 sequence => i+10
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=21
index=2 value=22
index=3 value=23
index=4 value=24
index=5 value=25
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50) index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 11 .. 15 sequence => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=ELEVEN
index=2 value=TWELVE
index=3 value=THIRTEEN
index=4 value=FOURTEEN
index=5 value=FIFTEEN
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50) index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 50 by 2 while i < 20 when i > 10 sequence => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=ELEVEN
index=2 value=THIRTEEN
index=3 value=FIFTEEN
index=4 value=SEVENTEEN
index=5 value=NINETEEN
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
declare
type tab_t is table of varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 sequence => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=ONE
index=2 value=TWO
index=3 value=THREE
index=4 value=FOUR
index=5 value=FIVE
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 index i => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
index=1 value=ONE
index=2 value=TWO
index=3 value=THREE
index=4 value=FOUR
index=5 value=FIVE
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' value=' || v);
end loop;
end;
/
Error report -
ORA-06550: line 6, column 18:
PLS-00868: The iterand type for an iteration control is not compatible with the collection index type, use SEQUENCE, or INDEX iterator association instead of a basic iterator association.
declare
type tab_t is table of varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 6 .. 10 index i => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || rpad(i,2,' ') || ' value=' || v);
end loop;
end;
/
index=1 value=
index=2 value=
index=3 value=
index=4 value=
index=5 value=
index=6 value=SIX
index=7 value=SEVEN
index=8 value=EIGHT
index=9 value=NINE
index=10 value=TEN
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of varchar2(50);
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in 1 .. 5 index i+5 => num_to_word(i)
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || rpad(i,2,' ') || ' value=' || v);
end loop;
end;
/
index=1 value=
index=2 value=
index=3 value=
index=4 value=
index=5 value=
index=6 value=ONE
index=7 value=TWO
index=8 value=THREE
index=9 value=FOUR
index=10 value=FIVE
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
select id, description, created_date
bulk collect into l_tab
from t1;
-- Display the contents of the collection.
for i in 1 .. l_tab.count loop
dbms_output.put_line('index=' || i || ' id=' || l_tab(i).id ||
' description=' || rpad(l_tab(i).description,8,' ') ||
' created_date=' || l_tab(i).created_date);
end loop;
end;
/
index=1 id=11 description=ELEVEN created_date=02-JAN-21
index=2 id=12 description=TWELVE created_date=02-JAN-21
index=3 id=13 description=THIRTEEN created_date=02-JAN-21
index=4 id=14 description=FOURTEEN created_date=02-JAN-21
index=5 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
for cur_rec in (select id, description, created_date from t1)
loop
l_tab(cur_rec.id) := cur_rec;
end loop;
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=11 id=11 description=ELEVEN created_date=02-JAN-21
index=12 id=12 description=TWELVE created_date=02-JAN-21
index=13 id=13 description=THIRTEEN created_date=02-JAN-21
index=14 id=14 description=FOURTEEN created_date=02-JAN-21
index=15 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in (select id, description, created_date from t1) index i.id => i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=11 id=11 description=ELEVEN created_date=02-JAN-21
index=12 id=12 description=TWELVE created_date=02-JAN-21
index=13 id=13 description=THIRTEEN created_date=02-JAN-21
index=14 id=14 description=FOURTEEN created_date=02-JAN-21
index=15 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in (select id, description, created_date from t1) when mod(i.id,2)=0 index i.id => i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=12 id=12 description=TWELVE created_date=02-JAN-21
index=14 id=14 description=FOURTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in (select id, description, created_date from t1) sequence => i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=1 id=11 description=ELEVEN created_date=02-JAN-21
index=2 id=12 description=TWELVE created_date=02-JAN-21
index=3 id=13 description=THIRTEEN created_date=02-JAN-21
index=4 id=14 description=FOURTEEN created_date=02-JAN-21
index=5 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
cursor c_cursor is
select id, description, created_date from t1;
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
begin
-- Populate the collection.
l_tab := tab_t(
for i in c_cursor index i.id => i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=11 id=11 description=ELEVEN created_date=02-JAN-21
index=12 id=12 description=TWELVE created_date=02-JAN-21
index=13 id=13 description=THIRTEEN created_date=02-JAN-21
index=14 id=14 description=FOURTEEN created_date=02-JAN-21
index=15 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
l_sql varchar2(32767);
l_cursor sys_refcursor;
begin
l_sql := 'select id, description, created_date from t1';
-- Populate the collection.
open l_cursor for l_sql;
l_tab := tab_t(
for i t1%rowtype in values of l_cursor index i.id => i
);
close l_cursor;
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
index=11 id=11 description=ELEVEN created_date=02-JAN-21
index=12 id=12 description=TWELVE created_date=02-JAN-21
index=13 id=13 description=THIRTEEN created_date=02-JAN-21
index=14 id=14 description=FOURTEEN created_date=02-JAN-21
index=15 id=15 description=FIFTEEN created_date=02-JAN-21
PL/SQL procedure successfully completed.
SQL>
declare
type tab_t is table of t1%rowtype index by pls_integer;
l_tab tab_t;
l_sql varchar2(32767);
begin
l_sql := 'select id, description, created_date from t1';
-- Populate the collection.
l_tab := tab_t(
for i t1%rowtype in (execute immediate l_sql) index i.id => i
);
-- Display the contents of the collection.
for i,v in pairs of l_tab loop
dbms_output.put_line('index=' || i || ' id=' || v.id ||
' description=' || rpad(v.description,8,' ') ||
' created_date=' || v.created_date);
end loop;
end;
/
Error report -
ORA-06550: line 13, column 33:
PLS-00801: internal error [*** ASSERT at file pdz4.c, line 3518; Self is null.; Xanon__0x1fc208ea0__AB[10, 38]]
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
SQL>Please to add comments
No comments yet. Be the first to comment!