An associative array in PL/SQL: key = varchar2, value = varray (varchar2)
I'm doing an associative array, where the key is a string and the value is an array of strings.I tried to declare it like this:
PROCEDURE myProcedure IS
tableau_donnees argument type is varray (200) of VARCHAR2 (20);
Column TYPE IS an ARRAY OF tableau_donnees INDEX BY VARCHAR2 (20);
my_collection columns; -collection of multi dimensional pl/sql
BEGIN
my_collection ('test') (1): = "Hello"; -It is the idea...
"my_collection ('test') (2): = ' 8128 is the number";
my_collection ("some other field' ') (1): = 'foo';
-etc etc.
But I get the error Oracle ORA-06531, reference to an uninitialized collection:
Cause: A service of element or member of a nested table or varray has been referenced (where an initialized collection is necessary) without the collection has been initialized.
Action: Initialize the collection with an appropriate constructor or assignment of the whole object.
I tried to use 'extend()' initialize the varray, but no help...
my_collection('test').extend (200);
-----
I also tried this:
PROCEDURE myProcedure IS
Data TYPE of TABLE IS OF VARCHAR2 (20);
Column TYPE IS an ARRAY OF data INDEX BY VARCHAR2 (20);
my_collection columns; -collection of multi dimensional pl/sql
But it's always the same. I am quite new to PL/SQL, if any help would be great!
-j
900685 wrote:
I'm doing an associative array, where the key is a string and the value is an array of strings.
create or replace
procedure myProcedure
is
type data_array
is
varray(200) of varchar2(20);
type coltype
is
table of data_array
index by varchar2(20);
my_collection coltype;
begin
my_collection('test') := data_array('hello','8128 is the number');
my_collection('some other field') := data_array('foo');
end;
/
And if you want to add elements in varray:
create or replace
procedure myProcedure
is
type data_array
is
varray(200) of varchar2(20);
type coltype
is
table of data_array
index by varchar2(20);
my_collection coltype;
begin
my_collection('test') := data_array('hello');
my_collection('test').extend;
my_collection('test')(2) := '8128 is the number';
my_collection('some other field') := data_array('foo');
end;
/
Or:
create or replace
procedure myProcedure
is
type data_array
is
varray(200) of varchar2(20);
type coltype
is
table of data_array
index by varchar2(20);
my_collection coltype;
begin
my_collection('test') := data_array(); -- initialize
my_collection('test').extend;
my_collection('test')(1) := 'hello';
my_collection('test').extend;
my_collection('test')(2) := '8128 is the number';
my_collection('some other field') := data_array('foo');
end;
/
SY.
Tags: Database
Similar Questions
-
Associative array of index files by varchar2 - PLS 00382
I have a SQL anonymous block that creates a report. At the end of each section, I need to summarize the data by "renting". The list of locations is finished (approx. 15 in a table) and not all sections will have data for each location, but I want the locations to order the same so I thought I'd use an associative array to store summary data. But when I try to initialize the associative array (I simply put the slots in the array in the "correct" order) I receive PLS-00382 expression is of the wrong type.
Here is the code (the lines where the error occurs are in bold):
TYPE summary_data () IS RENDERING
Summary1 NUMBER,
NUMBER of summary2
NUMBER of summary3
);
Summ_arr TYPE IS TABLE of summary_data INDEX OF VARCHAR2 (5);
location_summary summ_arr;
aLoc VARCHAR2 (5);
PROCEDURE init_summ (aSummArr IN OUT summ_arr) IS
BEGIN
aSummArr.Delete;
BEGIN
FOR in aLoc (select name - name is VARCHAR2 (5))
table
control key)
LOOP
aSummArr (aLoc) .summary_1: = 0;
aSummArr (aLoc) .summary_2: = 0;
aSummArr (aLoc) .summary_3: = 0;
END LOOP;
EXCEPTION
-exception handling
END;
END;
If this is not allowed, or I do something wrong? I tried substituting:
Summ_arr TYPE IS TABLE of summary_data INDEX OF VARCHAR2 (5);
with
Summ_arr TYPE IS TABLE of summary_data INDEX table.key%TYPE;
Nothing helps.
Thanks in advance for your help with this.
ANNUAL GENERAL MEETINGALOC is a record variable. You need to refrence the column in the file said. So, something like this:
aSummArr(aLoc.name).summary_1 := 0;
Cheers, APC
-
need help in the associative arrays indexed by varchar
Hi, I wanted to fill an associative array that is indexed by varchar, but I'm confused by incrementing the pointer so that the next lines gets inserted into the next available place.
sample data:
type ty_tb_tab_acc is record (account varchar2 (30));
type ty_tb_indx_acct is table of index ty_tb_tab_acc by varchar2 (35);
cursor sampls_cursor is
Select an account in sam_tab where the clause goes in here
say that my cursor returns 20 accounts. now, I want these 20 accounts will be inserted in the table associative ty_tb_indx_acct
How should I buckle and incrment table?
Please share your ideasuser13710379 wrote:
If this isn't the case, associatve tables approach I should use.
My requirement:
1.i will be 2 sliders who choose (type varchar2) account number according to diff where clause.
2. then, I need to compare these two cusrsor account number and fill out a folder or a collection, if the two account numbers are the same.This must be done using SQL. Not the tables or collections or associative arrays.
SQL is the MOST powerful language, that you have in the Oracle RDBMS. It is faster than PL/SQL, when it comes to crunch data. It is more scalable.
So why then pull data SQL in PL/SQL (expensive) memory? And then shoot some more SQL data in memory of PL/SQL (expensive)? And then use PL/SQL loop processing to compare the two sets of data?
SQL can do that very quickly and very efficiently - as it was designed for this specific purpose.
See the JOIN, INTERSECT , and LESS SQL operations to compare data sets.
-
Hi all
I searched through this forum trying to find I need information about the associative arrays with an index of varchar2 without success. What I'm looking for is a way to get the index or the 'key' values in the table without knowing what they are. Which means, I wouldn't know the index value in the design of the table, but would be able to use the values during execution. For those who are familiar with Java, it would be like calling the method keySet() of a map object.
So if I have a table of TYPE COLUMN_ARRAY IS TABLE OF VARCHAR2 (4000) INDEX OF VARCHAR2 (100) is it possible to dynamically get the index values without knowing what they are?
Any help is appreciated.
Thank youHello
declare i Number := 1; begin ... i := Array_List.Next(i); ...
Kind regards
Christian Balz
-
Associative arrays... items in the index
I can control the index of an associative array like that...
How can I control the index in the statement of collection in bulk as follows... so that I can delete records using this indexTYPE aat_id IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER; aa_ids aat_id; BEGIN aa_ids(1) := 3; aa_ids(2) := 8; aa_ids(2) := 10; aa_ids.delete(2); dbms_output.put_line(aa_ids.count); END;
Thank youSELECT decode(mod(rownum,2),1,1,2), object_name BULK COLLECT INTO aa_recs FROM all_objects WHERE ROWNUM <= 6;
HESH.HESH wrote:
but I have to play with the collected values, I want to delete the values in bulk without a loop through the list there at - it a trick possible to do this?
Using not PL/SQL and bays of PL/SQL at all - just using simple native SQL.
Before bulk collection and construction of an associative array, for the use of the values in table for future SQL operations, I rather to store the collection of values in a TWG (global temporary table) instead. A TWG can be indexed - and can thus provide much better access to the data as an associative array. He played better. It can be used via SQL, native mode.
The best place for the data is in the database. Not in the PL/SQL layer. This means that in SQL tables and not in PL/SQL tables or collections.
There are very few situations in PL/SQL, which require the use of associative arrays. If few, the majority of the PL/SQL code using associative arrays, IMO do badly. And what you've posted so far, I do not see a specific requirement for associative arrays.
So be sure you use the correct feature - and make sure that it is also well put to use in your code.
PS. HESH is a strange name. I'm used to HESH meaning High Explosive Squash Head (used for the filming of shielding). :-)
-
What is the preferred means of data transmission in the associative array of the nested table record vs
Choose between Nested Tables and associative arrays
The two nested tables and associative arrays (formerly index - by tables) use similar index notation, but they have different characteristics when it comes to persistence and ease of passing parameters.
Nested tables can be stored in a column of data, but can of associative arrays. Nested tables can simplify the SQL operations where you would normally join a single-column table with a larger table.
Associative arrays are appropriate for relatively small lookup tables where the collection can be constructed in memory whenever a procedure is called or a package is initialized. They are good for the collection of the information volume is unknown beforehand, because there is no fixed limit on their size. Their index values are more flexible, as associative array indices can be negative, can be no sequential and can use values of string instead of numbers.
PL/SQL automatically converts between the bays of the host and the associative arrays that use values of digital keys. The most effective way to move the collections to and from the database server is to implement data values in associative arrays, and then use these associative arrays with erections in bulk (the
FORALL
statement orBULK
COLLECT
clause).With the help of documents and Collections of PL/SQL
Read this:
-
Oracle 11.2.0.2.0
Using UTL_FILE, I can read the text file inot successfully in the associative array.
Now, I need to perform an advanced treatment of the values that is easily done with SQL, but for this the values should be in nested, no associative table. Is it possible a secret in another?
Thank you.
P.S. I am aware of the external tables and other means of data processing, but what I basically need is the ability to make SQL on associative arrays.declare infile utl_file.file_type; v_line varchar2(100); v_max varchar2(100); i integer:=1; type t_lines is table of varchar2(100) index by binary_integer; tab_lines t_lines; begin begin inFile := utl_file.fopen('ORADUMP','table.dat','R'); loop utl_file.get_line(infile,v_line); tab_lines(i) := v_line; i:=i+1; dbms_output.put_line(v_line); end loop; exception when no_data_found then utl_file.fclose(infile); when others then dbms_output.put_line(dbms_utility.format_error_backtrace); dbms_output.put_line('error ' || sqlcode || ' '||sqlerrm); end; --How to do something like this? --select max(column_value) into v_max from table(tab_lines); end; /
Nagornyi wrote:
I'd use a table nested from the beginning, but how to fill only one value at a time, during the reading of the file? Sorry, I didn't do well the answer in these examples...Basic idea
declare l_nested_table sys.odcivarchar2list default sys.odcivarchar2list(); begin l_nested_table.extend; l_nested_table(l_nested_table.count) := some_value; end; /
-
combine several pl/sql in a single associative array
Hello
I use oracle 10g, can anyone suggest me how to combine several pl/sql in a single Bay.
ex:
I want to combine the data in v_subnasp, v_product, v_order_type to v_combineTYPE t_array IS TABLE OF VARCHAR2 (100) INDEX BY BINARY_INTEGER; v_subnasp pk_rules.t_array; v_product pk_rules.t_array; v_order_type pk_rules.t_array; v_combine pk_rules.t_array;
DeepakDevarapalli wrote:
Sorry, I think that my question is not clear. can I move data from 3 tables on a Bay. and what is the best way. I found a way using loops.If you declare an associative array type - loop is pretty much your only option. If you declare the nested table type, you can use operatots of type multiset:
DECLARE TYPE t_array IS TABLE OF VARCHAR2(100); v_subnasp t_array := t_array(1); v_product t_array := t_array(2); v_order_type t_array := t_array(3); v_combine t_array; BEGIN v_combine := v_subnasp multiset union all v_product multiset union all v_order_type; for i in 1..v_combine.count loop dbms_output.put_line('Element ' || i || ' = ' || v_combine(i)); end loop; END; / Element 1 = 1 Element 2 = 2 Element 3 = 3 PL/SQL procedure successfully completed. SQL>
SY.
Published by: Solomon Yakobson, December 14, 2009 11:19
-
Error in the pl/sql block using associative arrays
Hello
I tried the following block of code using associative arrays.
and the error isDECLARE TYPE NumTab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; CURSOR c1 IS SELECT empno FROM emp; empnos NumTab; rows NATURAL := 10; BEGIN OPEN c1; FOR i in empnos.first..empnos.last LOOP /* The following statement fetches 10 rows (or less). */ FETCH c1 BULK COLLECT INTO empnos LIMIT rows; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE ( empnos.next(i)); END LOOP; CLOSE c1; END;
could you please let me know where I am going wrongDECLARE * ERROR at line 1: ORA-06502: PL/SQL: numeric or value error ORA-06512: at line 8
and please guide me where we use these associative arrays.
Thank youTry this
DECLARE TYPE NumTab IS TABLE OF emp%rowtype INDEX BY PLS_INTEGER; CURSOR c1 IS SELECT * FROM emp; empnos NumTab; rows INTEGER := 10; BEGIN OPEN C1; LOOP FETCH c1 BULK COLLECT INTO empnos LIMIT rows; EXIT WHEN c1%NOTFOUND; FOR i IN 1..empnos.count LOOP DBMS_OUTPUT.PUT_LINE(empnos(i).empno || '/' || empnos(i).ename); END LOOP; END LOOP; END;
-
Index associative arrays in varchar2
Hi all
It seems that associative arrays are indexes by varchar2
automatically sorted by index value.
Please run this test:
Now, the questions are:declare TYPE TEST_ARRAY IS TABLE OF VARCHAR2(10) INDEX BY VARCHAR2(10); aTest TEST_ARRAY; cIndex VARCHAR2(10); begin -- Test here aTest('X') := 'X'; aTest('A') := 'A'; aTest('C') := 'C'; aTest('Z') := 'Z'; aTest('C') := 'C'; aTest('B') := 'B'; aTest('AB') := 'AB'; aTest('12') := '12'; cIndex := aTest.FIRST; while cIndex IS NOT NULL LOOP dbms_output.put_line('aTest('||cIndex||')='||aTest(cIndex)); cIndex := aTest.NEXT( cIndex ); END LOOP; END;
1. are you aware of the documentation of Oracle that describe the behavior above?
2. are you aware of no example where associative arrays index in varchar2
is not sorted by its index? (Finally, except changing the NLS_SORT setting)
3. is it safe to use this feature of "sort"?
Thank you
Thomas1. are you aware of the documentation of Oracle that describe the behavior above?
" Associative arrays: ' indexes are stored in the sort order, not order of creation '."
-
Check if a value is in an associative array?
Hello
I am writing a process to plug into an existing infrastructure. I have an associative array of data (that I can't change the definition of) which is defined as:
type vc_arr2 is table of the varchar2 (32767) index directory.
An example of a data set can be:
Now I need to be able to see information indicating if a specific text value appears in the values, that is to say "is"Bananas"in the table? Now of course I could do it by writing a procedure loop over all the elements in order but there at - it do something built that? There are the obvious overhead (potentially) to loop through each element for each item, I'm looking for - I hope that there is something built in which more effective (IE, 'index' values or something like that).Key Text 1 => Apple 2 => Banana 3 => Pear 4 => Plum 5 => Grape etc etc
As far as I know, ARRAY. Exists() works on the key and not the value, and MEMBER OF cannot be used with associative arrays.
I'm not interested in what the key is for a given value (and I realize, it may be more than one key mapping to a given value), all I want is just if the element exists in the table.
Any help or suggestions would be greatly appreciated!No, you will need to loop through each of them, as your index finger is not on the text.
No reason to use an associative array in the first place? Why not use database and SQL tables? Oracle is good at those. ;)
-
Associative array in Oracle procedure problem
Hello
I searched through the internet and this forum and have not been able to solve a problem using the associative array of values in an IN clause. Everything I read says that I can not use the associative array directly in the SQL statement. I have to convert it to a table and then I can use it. Unfortunately, I get a ' ORA-21700: object does not exist or is marked for deletion "error when trying to access the table, I filled the table. Please note that I checked the table is actually filled during the loop. I capture the error when you are referencing in the SELECT statement.
I stated the following in the ARCHIVE package specification:
TYPE RSType IS REF CURSOR;
TYPE integer_aat IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
Integer_table TYPE TABLE IS OF INTEGER;
The procedure is the following:
PROCEDURE SEL_SEARCH_RESULTS (v_term IN VARCHAR2,
v_categories IN ARCHIVE.integer_aat,
RS ON RSType)
AS
/ * END: Returns the results for the category and key word provided
VARIABLES:
v_categories = array of categories of documents
v_term = keyword entered
RS = game results
*/
tbl_cat ARCHIVE.integer_table: = ARCHIVE.integer_table ();
BEGIN
BECAUSE me in 1... v_categories. COUNTY
LOOP
tbl_cat. EXTEND (1);
tbl_cat (i): = v_categories (i);
END LOOP;
OPEN FOR RS
SELECT A.ID,
B.CATEGORY,
A.FILENAME,
A.DISPLAY_NAME,
A.COMMENTS
OF TBL_ARCHIVE_DOCUMENTS,.
B TBL_ARCHIVE_DOC_CAT,
C TBL_ARCHIVE_DOC_KEYWORDS
WHERE A.ID = B.ID
AND A.ID = C.ID
AND B.CATEGORY in (SELECT * FROM TABLE (tbl_cat))
AND C.KEYWORD = v_term
ORDER BY A.ID;
END SEL_SEARCH_RESULTS;
Any help would be greatly appreciated and thanks in advance.
Matt905707 wrote:
Thanks for the quick response. I looked at the example suggest you and made the following changes. Now, I get an invalid 'Data Type' error on the "column_value SELECT FROM TABLE (CAST (tbl_cat AS tbl_integer))' statement. I must be missing something simple and I can't put my finger on it.You did not create a SQL type as I said. tbl_integer is still declared in the PLSQL code you have posted.
Reread my initial response and look at the example I posted (the very first thing that is done is to create a SQL type).
-
How to get the 'index' of an associative array?
Hello
Is it possible to get the 'index' of an associative array?
I saw say about 3000 ID.
Corresponding to each of these identifiers, thre is a 'key' (the 'key' can repeat for different ID)... tell key1, key2, key3.
Now, I want to show the top of page 8 keys ().
Could you please tell me how can I do this?
ConcerningDon't know what you are exactly looking for.
To get the index of an array of assotiative you should do something like
SQL> declare type arr is table of number index by varchar2 (5); var arr; idx varchar2 (5); begin -- fill the array for id in 1 .. 12 loop var ('key' || to_char (id, 'fm00')) := id; end loop; -- get the keys idx := var.first; dbms_output.put_line (idx); while idx is not null loop idx := var.next (idx); dbms_output.put_line (idx); end loop; end; / key01 key02 key03 key04 key05 key06 key07 key08 key09 key10 key11 key12 PL/SQL procedure successfully completed.
-
Using associative arrays in query
Hi all
Please see my schedule. It works very well
[CODE]
DECLARE
type mechanics_type_tab is table of index varchar2 (15) by varchar2 (15);
l_mechanics_type mechanics_type_tab;
L_BULLETIN t_tfo_wf_varchar_table:=t_tfo_wf_varchar_table();
l_count pls_integer: = 0;
BEGIN
FOR I IN (select bulletin_id, mechancs_type_id
of TFO_BULLETIN_PROMO_MECHANICS
where mechancs_type_id is not null) LOOP
l_count: = l_count + 1;
l_mechanics_type (i.bulletin_id): = i.mechancs_type_id;
l_mechanics_type (i.mechancs_type_id): = i.mechancs_type_id;
L_BULLETIN.extend;
L_BULLETIN (l_count): = i.bulletin_id;
END LOOP;
FOR J IN (SELECT TABLE COLUMN_VALUE (L_BULLETIN)) LOOP - this will change
DBMS_OUTPUT. Put_line ('COLUMN_VALUE =' | l_mechanics_type (J.COLUMN_VALUE));
END LOOP;
/ * does not
Insert into test_table (test1)
SELECT values l_mechanics_type (COLUMN_VALUE) OF TABLE (L_BULLETIN); */
END;
[/ CODE]
instead of the code below:
FOR J IN (SELECT TABLE COLUMN_VALUE (L_BULLETIN)) LOOP - this will change
DBMS_OUTPUT. Put_line ('COLUMN_VALUE =' | l_mechanics_type (J.COLUMN_VALUE));
END LOOP;
When I did as below
[CODE]
insert into test_table (test1) to
SELECT values l_mechanics_type (COLUMN_VALUE) OF TABLE (L_BULLETIN);
[/ CODE]
It does not work. Why it does not work.
Please note that in the real-world scenario, there are several fields to insert for test_table and test1 is one of them.
Just because the associative arrays are not supported in SQL before ORACLE 12 c.
HTH
-
Associative array as a type of database
As it is very well create an associative array inside a stored procedure:
type urltype is table of varchar2(32767) index by varchar2(30);
Can it be created as a type of database at the level of the schema?
Brian.McGinity wrote:
As it is very well create an associative array inside a stored procedure:
- type urltype is the table of index varchar2 (32767) by varchar2 (30);
Can it be created as a type of database at the level of the schema?
Non - associative arrays are a function of PL/SQL.
See table 5-1 in the doc of the PL/SQL language for a description of each type of collection and where (pl/sql, sql, etc.) they can be used.
http://docs.Oracle.com/CD/E11882_01/AppDev.112/e25519/composites.htm#CHDBHJEI
Maybe you are looking for
-
Should which library I use to remove old photos? iPhoto or Photos?
-
€100 on my ID apple lost?
Heey everybody, Today, I put 100 euros on my apple id account, but to use it, I need to check my security questions, but I don't know the answers more... I tried to send an email to reset responses, but electronic mail are not sent. Help please, than
-
How upgrade msata in Envy of Hp DV6 7211tx
I need information about the msata port and how to open the laptop to upgrade my msata to a new model. If not then I would like to serve is not where I'm going to buy a western digital ssd + HDD and use the ssd as drive C: Warranty has expired. HP DV
-
Over the range on USB-6008?
Dear jury, I wonder if an alternating signal operating at the limit of the range selected on the 6008 have on beach? Is the limit of the range a dc value; or is it over the range capabliliy built in AC RMS measures? The manual is not clear on this. S
-
> I went through all the steps and not the powercfg h on and it says this:> hibernation failed with the following error: the request is not supported.>> The following preventing hibernation on this system.> one or more older drivers are installed:> V