SQL on an associative array
Oracle 11.2.0.2.0Using 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.
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;
/
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.
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;
/
Tags: Database
Similar Questions
-
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;
-
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 difference between associative arrays and nested tables?
Hello
What is the difference between associative arrays and nested tables?
nested tables cannot be indexed by other than pls_integer and unlike nested tables table associative cananot be declared at the schema level.
is there any other difference set apart from the diff above 2?user13710379 wrote:
What is the difference between associative arrays and nested tables?Name-value pairs (associative) against a list of values (table standard/nested table).
nested tables cannot be indexed by other than pls_integer
They are not "indexed" the way in which an associative array is indexed. A standard table is referenced by the position of the cell in the table. This position is essentially the offset of the memory of the cell from the start address of the table.
Can not solve a cell in an associative array directly via a memory offset index. You place a cell reference value it by his 'name' (a search in the linked list/hash table).
The following example shows the difference between the pairs of name / value and a list of core values.
SQL> declare 2 --// associative arrays are NAME-VALUE pairs 3 type TArr1 is table of varchar2(10) index by pls_integer; 4 type TArr2 is table of varchar2(10) index by varchar2(10); 5 6 arr1 TArr1; 7 arr2 TArr2; 8 begin 9 arr1(100) := '1st entry'; 10 arr1(1) := '2nd entry'; 11 arr1(5) := '3rd entry'; 12 13 arr2('john') := 'New York'; 14 arr2('jane') := 'Paris'; 15 arr2('jack') := 'London'; 16 17 end; 18 / PL/SQL procedure successfully completed. SQL> SQL> SQL> declare 2 --// standard arrays are lists 3 type TArr3 is table of varchar2(10); 4 type TArr4 is table of number; 5 6 arr3 TArr3; 7 arr4 TArr4; 8 begin 9 arr3 := new TArr3( '1st entry', '2nd entry', '3rd entry' ); 10 11 arr4 := new TArr4( 100, 1, 5 ); 12 end; 13 / PL/SQL procedure successfully completed. SQL>
-
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:
-
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!
-j900685 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.
-
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
-
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
-
I have a table proj_test .i want to recover the data in the associative array.
How to do?
create table proj_test as select 1 as id, 1 as rn, 'Fred' as nm from dual union all select 1,2,'Bloggs' from dual union all select 2,1,'Scott' from dual union all select 2,2,'Smith' from dual union all select 3,1,'Jim' from dual union all select 3,2,'Jones' from dual
You need not Associative array if you do a bulk of collect. You can use the nested PL/SQL table type.
11g and more
declare
is of type tbl table of the proj_test % rowtype;
tbl l_tbl;
Start
SELECT id, rn, nm bulk collect into l_tbl
of proj_test;
because me in 1.l_tbl.count
loop
dbms_output.put_line
(
RPAD (to_char (l_tbl (i) USER.USER), 10, ' ')
|| RPAD (to_char (l_tbl (i). (RN), 10, ' ')
|| l_tbl (i) .nm
);
end loop;
end;
Before 11 g
declare
type id_tbl is table of the proj_test.id%type;
type rn_tbl is table of the proj_test.rn%type;
type nm_tbl is table of the proj_test.nm%type;
id_tbl l_id;
l_rn rn_tbl;
l_nm nm_tbl;
Start
SELECT id, rn, nm bulk collect into l_id, l_rn, l_nm
of proj_test;
because me in 1.l_id.count
loop
dbms_output.put_line
(
RPAD (to_char (l_id (i)), 10, ' ')
|| RPAD (to_char (l_rn (i)), 10, ' ')
|| l_nm (i)
);
end loop;
end;
-
Error renaming dates to an associative array of type date
Hi all
I am facing issue while assigning dates in an associative array of type date:
Oracle version:
SQL > select * from v version $;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
AMT for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - ProductionStored procedure, I try to write is as follows
- create or replace procedure (jp1)
- p_start_date default date trunc(sysdate,'MM')
- p_end_date date default trunc (sysdate)
- )
- is
- number of l_no_of_days;
- type t_date_id is table of date
- index by pls_integer;
- l_date_id_arr t_date_id;
- Start
- l_no_of_days: = p_end_date - p_start_date;
- for i from 0
- .. l_no_of_days - 1
- loop
- l_date_id_arr: = p_start_date + i;
- dbms_output.put_line (p_start_date + i);
- end loop;
- end;
- /
I get error on line 14 when compiling it. and the error message is as follows:
JP1 PROCEDURAL errors:
LINE/COL ERROR
-------- -----------------------------------------------------------------
14/5 PL/SQL: statement ignored
14/22 PLS-00382: expression is of the wrong typeWhile studying this, I tried the value of (p_start_date + i) using dbms_output.put_line and the output is so date itself.
- create or replace procedure (jp1)
- p_start_date default date trunc(sysdate,'MM')
- p_end_date date default trunc (sysdate)
- )
- is
- number of l_no_of_days;
- type t_date_id is table of date
- index by pls_integer;
- l_date_id_arr t_date_id;
- Start
- l_no_of_days: = p_end_date - p_start_date;
for i from 0... l_no_of_days-1
- loop
- -l_date_id_arr: = p_start_date + i;
- dbms_output.put_line (p_start_date + i);
- end loop;
- end;
- /
output of the
- exec jp1
is as follows:
1ST DECEMBER 13
2 DECEMBER 13
3 DECEMBER 13
DECEMBER 4, 13
5 DECEMBER 13
DECEMBER 6, 13
7 DECEMBER 13
DECEMBER 8, 13
9 DECEMBER 13
DECEMBER 10, 13
DECEMBER 11, 13
DECEMBER 12, 13
13 DECEMBER 13
14 DECEMBER 13
15 DECEMBER 13
16 DECEMBER 13
17 DECEMBER 13
18 DECEMBER 13
I see the release date itself. so why he throws error while the same assignment to an associative array of type date.
I tried Google too for the same but without success.
Any help is appreciated in this regard not or any pointer another thread on the internet or in this forum.
Thanks in advance
Jagdeep Seven
Read associative arrays:
- create or replace procedure (jp1)
- p_start_date default date trunc(sysdate,'MM')
- p_end_date date default trunc (sysdate)
- ) is
- number of l_no_of_days;
- type t_date_id is table of date
- index by pls_integer;
- l_date_id_arr t_date_id;
- Start
- l_no_of_days: = p_end_date - p_start_date;
- because me in 0.l_no_of_days - 1
- loop
- l_date_id_arr (I): = p_start_date + i;
- dbms_output.put_line (p_start_date + i);
- end loop;
- end;
----
Ramin Hashimzade
-
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
-
Cannot run the autonomous block for input of associative array parameter
Hello
I have created an associative array in a package-PKG_CMT_DAP
TYPE r_supply_type_id IS RECORD( supply_type_id varchar2(50)); TYPE t_supply_type_id IS TABLE OF r_supply_type_id INDEX BY BINARY_INTEGER; l_supply_type_id t_supply_type_id;
Later created a test procedure with an input of the above kind parameter
create or replace procedure test_prc(p_in_st_id IN PKG_CMT_DAP.t_supply_type_id) AS v_input1 number; v_input2 number; BEGIN FOR z IN 1..p_in_st_id.COUNT LOOP DBMS_output.put_line(p_in_st_id(z)supply_type_id); END LOOP; END test_prc;
How to update successfully.
Created a stand-alone as block below
DECLARE BEGIN test_prc(p_in_st_id=>(100,200,300)); END;
Above am past in 100,200,300 as an input string, and the expected output should display all the numbers above on the screen.
But it throws the below error
Error report: ORA-06550: line 3, column 1: PLS-00306: wrong number or types of arguments in call to 'TEST_PRC' ORA-06550: line 3, column 1: PL/SQL: Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:
Could you please help me where I am doing wrong?
Kind regards
Claudy Kotekal
Claudy,
While calling the procedure, you must declare the variable of type table. Give the values for the elements in the begin block and then pass that variable in the procedure. Something like below:
Declare
in_array PKG_CMT_DAP. r_supply_type_id;
Begin
in_array (1) .supply_type_id: = 100;
in_array (2) .supply_type_id: = 200;
in_array (3) .supply_type_id: = 300;
test_prc (in_array);
end;
NOTE: There might be a few problems with my code syntax, but logic would be same.
Try this and let me know.
Thank you
-
An associative array, how the records using the loop counter?
In the associative array, how the records using the loop counter? for example
declare type population is table of number index by varchar2(64); city_population population; begin city_population('Samillve') := 200; city_population('Lindenhurst') := 300; for i in 1 .. city_population.count loop dbms_output.put_line(city_population(i)); -- compiler error end loop; end; /
That would look like
SQL> ed Wrote file afiedt.buf 1 declare 2 type population is table of number index by varchar2(64); 3 city_population population; 4 l_index varchar2(64); 5 begin 6 city_population('Samillve') := 200; 7 city_population('Lindenhurst') := 300; 8 l_index := city_population.first; 9 while( l_index IS NOT NULL ) 10 loop 11 dbms_output.put_line(city_population(l_index )); 12 l_index := city_population.next(l_index); 13 end loop; 14* end; SQL> / 300 200 PL/SQL procedure successfully completed.
Justin
-
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
-
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. ;)
Maybe you are looking for
-
The value of path as unit target in the calculation Manager
Hello is there a way to define a course of treatment as unit target in the calculation Manager? I have data with unspecified units (for example, deg_Cel ° C) then I disabled the calculation of the amount in function. Now I want the result channel hav
-
Wow I hate to say it but im glad will not only through this
my vista computer is constantly popping up boxes telling me to update ATI graphics driver or enable the adapter, then I wanted to get rid of some things in my Start menu & tried to go to windows defender & my PC can't find it 'what' where did it go?
-
Skype cannot find my sound card
Skype cannot find my Sound card - when I run the test of the Echo - it I heard say 'Hello' then it dies. I have a brand new Thinkpad (T420), Windows 7, internal camera and microphone, Windows loaded all drivers. I tried to find the communication help
-
Download InDesign to my MacBook for the repair of my iMac?
Hi allLicensed to Adobe, I can download a copy of InDesign v.? on my MacBook without charge, then my iMac (in which InDesign is installed), is being repaired?