Created
May 31, 2019 20:18
-
-
Save nochmu/6656401a7ac78760c973fa7eaec655e3 to your computer and use it in GitHub Desktop.
Some basic notes about PL/SQL Nested Tables (aka. List, Set, Bag)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE | |
-- This example shows the basic usage and behavior of the Nested List. | |
TYPE t_list IS table of integer; | |
v_list t_list; | |
v_max_size integer := 0; | |
PROCEDURE print_count_and_last IS | |
BEGIN | |
DBMS_OUTPUT.PUT('COUNT = ' || v_list.COUNT || ', '); | |
DBMS_OUTPUT.PUT_LINE('LAST = ' || nvl(TO_CHAR(v_list.last),'NULL')); | |
END; | |
PROCEDURE example(p_text IN varchar2 := null) IS | |
BEGIN | |
dbms_output.put_line('-- '||p_text); | |
END; | |
PROCEDURE dump_list IS | |
v_end number := coalesce(v_list.last, v_list.count); | |
BEGIN | |
IF v_end > v_max_size THEN | |
v_max_size := v_end; | |
END IF; | |
print_count_and_last(); | |
FOR i IN 1..v_max_size LOOP | |
IF v_list.exists(i) THEN | |
IF v_list(i) is not null THEN | |
dbms_output.put_line('('||i||') = '||v_list(i)); | |
ELSE | |
dbms_output.put_line('('||i||') = NULL'); | |
END IF; | |
ELSE | |
dbms_output.put_line('('||i||') does not exists'); | |
END IF; | |
END LOOP; | |
DBMS_OUTPUT.PUT_line(''); | |
END; | |
BEGIN | |
example('Empty list'); | |
v_list := t_list(); | |
dump_list(); | |
example('Add Elements via Constructor' ); | |
v_list := t_list(1, 2, 3, 4); | |
dump_list(); | |
example('Append a null element'); | |
v_list.EXTEND(); | |
dump_list(); | |
example('Append a not-null element'); | |
v_list.EXTEND(); | |
v_list(v_list.last) := 6; | |
dump_list(); | |
example('Delete Element at index 3'); | |
v_list.DELETE(3); | |
dump_list(); | |
example('Delete the first element'); | |
v_list.DELETE(v_list.first); | |
dump_list(); | |
example('Trim one element - removes the element from end'); | |
v_list.TRIM(1); | |
dump_list(); | |
-- Your next step is to read about SET operators. Very cool stuff! | |
-- https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/plsql-collections-and-records.html#GUID-E939651F-5E2B-4A83-9035-4FA5498B78AA | |
-- https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/plsql-collections-and-records.html#GUID-C8C96B4E-09BE-476F-A95C-D2D0B1589CB8 | |
END; | |
/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment