ORA-06533: Subscript beyond count with associative array - arrays

I am getting Subscript beyond count error while executing the procedure. I am trying to populate an associative array in my procedure and then eventually use it to insert. I think there is somewhere error in initialization of the array
Type definitions in a package header
TYPE sonic_data_rec IS RECORD(
ep_a_num LOG_PICK.EP_A_NUM%TYPE,
wvy_num LOG_PICK.WVY_NUM%TYPE,
ltrl_name LOG_PICK.Ltrl_Name%TYPE,
vel_pick_sq_num LOG_PICK.w_vel_pick_sq_num%TYPE,
vel_sv_typ_cd LOG_PICK.W_VEL_SV_TYP_CD%TYPE,
vel_sv_dt LOG_PICK.W_VEL_SV_DT%TYPE,
w_vel_log_pick_cd LOG_PICK.w_vel_log_pick_cd%TYPE,
sq_num LOG_PICK.W_VEL_PICK_SQ_NUM%TYPE,
pick_cd LOG_PICK.W_VEL_LOG_PICK_CD%TYPE,
onew_tm LOG_PICK.W_VEL_ONE_WAY_TV_TM%TYPE,
sonic_log LOG_PICK.W_VEL_SOLOG_CALB_VSP%TYPE,
ms_dpth LOG_PICK.W_VEL_PICK_DPTH%TYPE,
tv_dpth LOG_PICK.W_VEL_PICK_DPTH%TYPE);
TYPE sonic_tab IS TABLE OF sonic_data_rec;
Procedure code, only relevant code of package_body posted
PROCEDURE LOAD_LOG_PICK
(p_wvy_num IN WELL_VEL_SURVEY.WVY_NUM%TYPE
,p_noofpts IN NUMBER
,p_data_list IN STRINGARRAY)
IS
l_son_array sonic_tab := sonic_tab();
BEGIN
count1 := 1;
LOOP
l_son_array(count1).ep_a_num := p_ep_a_num;
l_son_array(count1).wvy_num := p_wvy_num;
l_son_array(count1).vel_sv_typ_cd := 'L';
l_son_array(count1).vel_sv_dt := p_pick_date;
l_son_array(count1).vel_pick_sq_num := count1;
l_son_array(count1).w_vel_log_pick_cd := 'O';
l_son_array(count1).ms_dpth := l_ms_dpth;
l_son_array(count1).onew_tm := regexp_substr(p_data_list(count1), '[^ ]+', 1, 1);
l_son_array(count1).sonic_log := regexp_substr(p_data_list(count1), '[^ ]+', 1, 1);
EXIT WHEN count1=5000;
count1 := count1+1;
END LOOP;

You are attempting to populate a nested table, not an associative array.
If sonic_tab was an associative array, it would be declared as
TYPE sonic_tab IS TABLE OF sonic_data_rec INDEX BY BINARY_INTEGER;
See the Oracle documentation on PL/SQL collection types for further information about the different types of collections you can use in PL/SQL.
As you have a nested table, you need to add calls to sonic_tab.EXTEND to make the table larger. It starts with size 0 because when you create it by calling sonic_tab(), you aren't passing any records into the constructor. Either call sonic_tab.EXTEND(1); in each iteration of the loop before you try to add anything to that item of the table, or call sonic_tab.EXTEND(5000); once before the loop if you know in advance how many items you will have in the table.

Related

set cap length for concatenated string

EDIT: I'm having alot of trouble formatting this code for some reason please bear with me. Also i'm aware some code is missing. This is just one portion of the code.
I am simulating a batch load that is run nightly to do some load testing. The problem I face is that my auto-generated PK's exceed the columns datalength after 100 or so inserts. How would I cap off my Strings without violating the unique constraint while inserting around 20,000 rows per table. my goal is to get rid of the random strings due to a change in requirements.Below is the portion of code I'm having trouble with.
declare
l_cnt integer := 0;
t_cnt integer := 0;
c_cnt integer := 0;
f_cnt integer := 0;
i integer := 0;
TYPE T_EMPL_NO IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
TAB_EMPL_NO T_EMPL_NO;
TAB_SEC_PK T_EMPL_NO;
TAB_THR_PK T_EMPL_NO;
TAB_FTH_PK T_EMPL_NO;
begin
dbms_output.put_line('START LOAD TEST');
LOOP
i := i + 1;
TAB_EMPL_NO(l_cnt) := 'JB'||i;
TAB_SEC_PK(t_cnt) := dbms_random.string('L',6);
TAB_THR_PK(c_cnt) := dbms_random.string('L',1);
TAB_FTH_PK(f_cnt) := dbms_random.string('L',20);
Insert into AOMS.PARTS_MONTH_CLOSE(
NAMES OF COLUMNS HERE
) Values (
TAB_EMPL_NO(l_cnt),
TAB_SEC_PK(t_cnt),
TAB_THR_PK(c_cnt),
TAB_FTH_PK(f_cnt)
);
l_cnt := l_cnt + SQL%ROWCOUNT;
EXIT WHEN l_cnt = 100; -- change to record count 22k
END LOOP;
dbms_output.put_line('P2ACCTMO :Rows inserted: ' || l_cnt);
END;
/
As a bonus If I wanted the script to run for an hour but not exceed the amount of records that insert during a loop how would I do that? Thanks so much for any help.
To define a numeric datatype with a given range (let's say 1 to 1000, for the sake of argument) you can use a user-defined PL/SQL subtype. To do this you'd use something like
SUBTYPE MY_ZERO_TO_1K_SUBTYPE IS NUMBER(4,0) RANGE 0..1000;
and then define variables of this subtype just as you would any other variable:
nLimited_number MY_ZERO_TO1K_SUBTYPE;
You can set a variable of this type to 0, 1, 2, ..., 998, 999, 1000. However, if you set it to a negative value or a value greater than 1000 an ORA-06502: PL/SQL: numeric or value error exception will be raised.
I would suggest to go for SEQUENCES rather than using Random string
generation. In this way you wlll not face unique key violation and
also you can set the max seq limit to the datatype limit of the
column. Hope this information helps.

Remove duplicates from associative array with multiple columns in PL/SQL

I have seen several great examples of removing duplicates in an Oracle associative array using MULTISET UNION DISTINCT and SET. It works great when there is only a single column.
I have an associative array based on a RECORD type which contains 3 columns.
Is it possible to use the methods mentioned above?
DECLARE
TYPE rec_type IS RECORD(
column1 VARCHAR2(5)
,column2 VARCHAR2(5));
TYPE my_aa IS TABLE OF rec_type;
p_tbl my_aa := my_aa();
q_tbl my_aa := my_aa();
BEGIN
p_tbl.extend(4);
p_tbl(1).column1 := 'A1';
p_tbl(1).column2 := 'a';
--
p_tbl(2).column1 := 'A1';
p_tbl(2).column2 := 'b';
--
p_tbl(3).column1 := 'A1'; -- Dup
p_tbl(3).column2 := 'a'; -- Dup
--
p_tbl(4).column1 := 'A1';
p_tbl(4).column2 := 'c';
--
dbms_output.put_line('-- First output contains duplicated');
--
FOR a IN p_tbl.first .. p_tbl.last LOOP
dbms_output.put_line(a || ' = ' || p_tbl(a).column1 || ' / ' || p_tbl(a).column2);
END LOOP;
--
--
q_tbl := p_tbl MULTISET UNION DISTINCT p_tbl;
--
--
dbms_output.new_line;
dbms_output.put_line('-- Duplicates have been removad');
FOR a IN q_tbl.first .. q_tbl.last LOOP
dbms_output.put_line(a || ' = ' || q_tbl(a).column1|| ' / '||q_tbl(a).column2);
END LOOP;
END;
It is impossible to do distinction by MULTISET UNION DISTINCT with underlying RECORD type due to doc
The element types of the nested tables must be comparable. Please
refer to "Comparison Conditions " for information on the comparability
of nonscalar types.
https://docs.oracle.com/cd/B12037_01/server.101/b10759/operators006.htm
Records are not comparable. You can use user defined objects with MAP method for this
Two objects of nonscalar type are comparable if they are of the same
named type and there is a one-to-one correspondence between their
elements. In addition, nested tables of user-defined object types,
even if their elements are comparable, must have MAP methods defined
on them to be used in equality or IN conditions.
https://docs.oracle.com/cd/B12037_01/server.101/b10759/conditions002.htm#i1033286
If you still whant do this with records then try SELECT DISTINCT ... FROM TABLE() approach, but your types must be defined on schema level for server older than oracle 12c.

Error for Associative Array : Missing IN OUT Parameter

I'm learning about Collections and trying out Associative Arrays in Oracle 11g. I'm using SQL Developer to write and test my code below and I am getting the error which I can't troubleshoot :
Error Report
Missing IN OUT Parameter at index ::1
Code I have written is as follows:
---SIMPLE collections EXAMPLE
DECLARE
TYPE prospect_towns IS TABLE OF VARCHAR2 (25)
INDEX BY PLS_INTEGER;
a_big_towns prospect_towns; -- associative array
i PLS_INTEGER := 1; -- index for the array
v_counter NUMBER;
v_town VARCHAR2(25);
BEGIN
a_big_towns(1):='Birmingham';
a_big_towns(2):='London':
a_big_towns(3):='Manchester';
-- v_counter := 1;
FOR i IN 1..a_big_towns.COUNT
LOOP <<big towns>>
--v_town := a_big_towns(i);
DBMS_OUTPUT.PUT_LINE('Inside Loop, town is '||a_big_towns(i));
i= a_big_towns.next:
END LOOP<<big towns>>
END;
/
Any ideas what's wrong ?
The second of these lines:
a_big_towns(1):='Birmingham';
a_big_towns(2):='London':
a_big_towns(3):='Manchester';
... has a colon at the end, instead of a semicolon. That's causing the following a_big_towns to be interpreted as a bind variable name by the parser. So it should be:
a_big_towns(2):='London';
Once you get past that, this line isn't needed, and would need := instead of = if it was, and also has a colon instead of a semicolon at the end:
i= a_big_towns.next:
... so remove that completely.
I'm not sure the labels are really adding anything here, but if you do have a label it doesn't need to be repeated at the end, and the name can't have a space in it, so make it:
<<big_towns>>
FOR i IN 1..a_big_towns.COUNT LOOP
And this needs a semicolon at the dned:
END LOOP;
This SQL Fiddle compiles.

Insert DBGrid data into a multi-dimensional array

I have set a connection from Delphi to pgsql using ADOConnection, ADOQuery, DataSource and a DBGrid to present the results of my query.
The database contains 2 columns of values of type double, of some thousands of rows, which I would like to insert into a two-dimensional array.However, as am quite new I am not sure how to insert the contents of a DBGrid into an array. Any help much appreciated.
First of all if there are thousands of rows you need to assign fields into variables before reading to get rid of unnecessary text lookup time when using FieldByName.
I dont't have Delphi at hand but this should work or at least help you get started.
uses Math;
procedure ProcessArray(ADataSet: TDataSet);
var
field1: TField;
field2: TField;
len: Integer;
a: array of array[2] of double;
begin
len := 0;
SetLength(a, 0);
field1 := ADataSet.FieldByName('field1');
field2 := ADataSet.FieldByName('field2');
ADataSet.First;
while not ADataSet.Eof do
begin
Inc(len);
if len > Length(a) then
SetLength(a, len + Min(len, 16384));
a[len - 1][0] := field1.Value;
a[len - 1][1] := field2.Value;
ADataSet.Next;
end;
SetLength(a, len);
// Process the results in the a array
end;
What AlexSC is suggesting is to actually use TADODataSet.RecordCount property to set the size of an array initially. Please note that if TDataSet is not loaded completely from the database (uses server cursor for example), RecordCount will not necessarily contain the number of all records and the original solution above would be able to cope with this. I made a correction to it so it won't grow more than 16k items at once and the overhead will be at most 16k - 1 array entries. For information about TDataSet "lazy loading" refer to DBGrid with read ahead capability using ADO
Please find the code using RecordCount below:
procedure ProcessArray(ADataSet: TDataSet);
var
field1: TField;
field2: TField;
len: Integer;
a: array of array[2] of double;
begin
len := 0;
SetLength(a, ADataSet.RecordCount);
field1 := ADataSet.FieldByName('field1');
field2 := ADataSet.FieldByName('field2');
ADataSet.First;
while not ADataSet.Eof do
begin
a[len][0] := field1.Value;
a[len][1] := field2.Value;
Inc(len);
ADataSet.Next;
end;
// Process the results in the a array here
end;

What data structure to use in order to sort this data in PL/SQL?

This is Oracle 11.2g. In a PL/SQL function, I've got a loop whereby each iteration, I create a string and an integer associated with that string. The function returns the final concatenation of all the generated strings, sorted (depending on a function input parameter), either alphabetically or by the value of the integer. To give an idea, I'm generating something like this:
Iteration String Integer
1 Oslo 40
2 Berlin 74
3 Rome 25
4 Paris 10
If the input parameter says to sort alphabetically, the function output should look like this :
Berlin, Oslo, Paris, Rome
Otherwise, we return the concatenated strings sorted by the value of the associated integer:
Paris, Rome, Oslo, Berlin
What is the most appropriate data structure to achieve this sort? I've looked at collections, associative arrays and even varrays. I've been kind of shocked how difficult this seems to be to achieve in Oracle. I saw this question but it doesn't work in my case, as I need to be able to sort by both index and value: How to sort an associative array in PL/SQL? Is there a more appropriate data structure for this scenario, and how would you sort it?
Thanks!
It is very easy if you use PL/SQL as SQL and not like other languages. It is quite specific and sometimes is very nice exactly because of that.
Sometimes I really hate PL/SQL, but this case is absolutely about love.
See how easy it is:
create type it as object (
iter number,
stringval varchar2(100),
intval integer
);
create type t_it as table of it;
declare
t t_it := new t_it();
tmp1 varchar2(32767);
tmp2 varchar2(32767);
begin
t.extend(4);
t(1) := new it(1,'Oslo',40);
t(2) := new it(2,'Berlin',74);
t(3) := new it(3,'Rome',25);
t(4) := new it(4,'Paris',10);
select listagg(stringval,', ') within group (order by stringval),
listagg(stringval,', ') within group (order by intval)
into tmp1, tmp2
from table(t);
dbms_output.put_line(tmp1);
dbms_output.put_line(tmp2);
end;
/
drop type t_it;
drop type it;
Here you can see the problem that you must create global types, and this is what I hate it for. But they say in Oracle 12 it can be done with locally defined types so I am waiting for it :)
The output is:
Berlin, Oslo, Paris, Rome
Paris, Rome, Oslo, Berlin
EDIT
As far as you do not know the amount of iterations from the beginning the only way is to do extend on each iteration (this is only example of extending):
declare
iterator pls_integer := 1;
begin
/* some type of loop*/ loop
t.extend();
-- one way to assign
t(t.last) := new it(1,'Oslo',40);
-- another way is to use some integer iterator
t(iterator) := new it(1,'Oslo',40);
iterator := iterator + 1;
end loop;
end;
I prefer the second way because it is faster (does not calculate .last on each iteration).
This is an example of pure PL/SQL implementation that is based on the idea associative array (aka map or dictionary in other domains) is an ordered collection that is sorted by a key. That is a powerful feature that I have used multiple times. For input data structure in this example I decided to use a nested table of records (aka a list of records).
In this particular case however I'd probably go for similar implementation than in simon's answer.
create or replace package so36 is
-- input data structures
type rec_t is record (
iter number,
str varchar2(20),
int number
);
type rec_list_t is table of rec_t;
function to_str(p_list in rec_list_t, p_sort in varchar2 default 'S')
return varchar2;
end;
/
show errors
create or replace package body so36 is
function to_str(p_list in rec_list_t, p_sort in varchar2 default 'S')
return varchar2 is
v_sep constant varchar2(2) := ', ';
v_ret varchar2(32767);
begin
if p_sort = 'S' then
-- create associative array (map) v_map where key is rec_t.str
-- this means the records are sorted by rec_t.str
declare
type map_t is table of rec_t index by varchar2(20);
v_map map_t;
v_key varchar2(20);
begin
-- populate the map
for i in p_list.first .. p_list.last loop
v_map(p_list(i).str) := p_list(i);
end loop;
v_key := v_map.first;
-- generate output string
while v_key is not null loop
v_ret := v_ret || v_map(v_key).str || v_sep;
v_key := v_map.next(v_key);
end loop;
end;
elsif p_sort = 'I' then
-- this branch is identical except the associative array's key is
-- rec_t.int and thus the records are sorted by rec_t.int
declare
type map_t is table of rec_t index by pls_integer;
v_map map_t;
v_key pls_integer;
begin
for i in p_list.first .. p_list.last loop
v_map(p_list(i).int) := p_list(i);
end loop;
v_key := v_map.first;
while v_key is not null loop
v_ret := v_ret || v_map(v_key).str || v_sep;
v_key := v_map.next(v_key);
end loop;
end;
end if;
return rtrim(v_ret, v_sep);
end;
end;
/
show errors
declare
v_list so36.rec_list_t := so36.rec_list_t();
v_item so36.rec_t;
begin
v_item.iter := 1;
v_item.str := 'Oslo';
v_item.int := 40;
v_list.extend(1);
v_list(v_list.last) := v_item;
v_item.iter := 2;
v_item.str := 'Berlin';
v_item.int := 74;
v_list.extend(1);
v_list(v_list.last) := v_item;
v_item.iter := 3;
v_item.str := 'Rome';
v_item.int := 25;
v_list.extend(1);
v_list(v_list.last) := v_item;
v_item.iter := 4;
v_item.str := 'Paris';
v_item.int := 10;
v_list.extend(1);
v_list(v_list.last) := v_item;
dbms_output.put_line(so36.to_str(v_list));
dbms_output.put_line(so36.to_str(v_list, 'I'));
end;
/
show errors

Resources