| sorting a nested table/collection [message #530810] |
Thu, 10 November 2011 14:23  |
shijumic
Messages: 31 Registered: May 2010
|
Member |
|
|
In test.pks file I declared the following type
TYPE tab_tests is table of NUMBER(15);
In test.pkb, I have the following procedure
PROCEDURE report (
i_cid IN NUMBER
)
IS
test1 tab_tests := tab_tests();
test2 tab_tests;
BEGIN
-- I populate test1 with the data and it works fine
-- But when I tried to make a sorted nested table with the following command
I got this error 'ORA-00902: invalid datatype', is there any other efficient way to sort
a nested table than the following, like a built in Oracle collection method?
execute immediate 'select cast( multiset (select * from table(test1) order by 1) as tab_tests) INTO test2 FROM dual';
END;
[Updated on: Thu, 10 November 2011 14:48] by Moderator Report message to a moderator
|
|
|
|
|
|
| Re: sorting a nested table/collection [message #530812 is a reply to message #530811] |
Thu, 10 November 2011 14:46   |
shijumic
Messages: 31 Registered: May 2010
|
Member |
|
|
I am not a PL/SQL guy, I am using Nested table/collection so that I can store the temporary/staging data in it to create some reports. I was told by Oracle DBA in my company not to use gtt or something in the procedure and I am pretty new to PL/SQL. Is there any way I can sort the data in a nested table and loop through it to do some calculations? some example would be really helpful.
Thanks!
|
|
|
|
|
|
| Re: sorting a nested table/collection [message #530817 is a reply to message #530814] |
Thu, 10 November 2011 15:29   |
shijumic
Messages: 31 Registered: May 2010
|
Member |
|
|
I tried your approach, but it errored in the following line
select * bulk collect into test2 from table(test1) order by 1;
PL/SQL: ORA-22905: cannot access rows from a non-nested table
item
PLS-00642: local collection types not allowed in SQL statements
|
|
|
|
|
|
| Re: sorting a nested table/collection [message #530820 is a reply to message #530817] |
Thu, 10 November 2011 16:14   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
shijumic wrote on Thu, 10 November 2011 16:29I tried your approach
No you did not. Otherwise you would not get "local collection types not allowed in SQL statements" which tells me you still declaring tab_tests in PL/SQL. You need:
a) declare it in SQL like Michel did
b) remove type tab_tests declaration from the package since it takes precedence over SQL declared type. Or prefix type name with type schema name to explicitly tell PL/SQl which tab_tests type you have in mind:
SQL> create type tab_tests is table of NUMBER(15);
2 /
Type created.
SQL> create or replace package pkg1
2 as
3 type tab_tests is table of NUMBER(15);
4 PROCEDURE report(i_cid IN NUMBER
5 );
6 end;
7 /
Package created.
SQL> create or replace package body pkg1
2 as
3 PROCEDURE report (
4 i_cid IN NUMBER
5 )
6 IS
7 test1 tab_tests := tab_tests();
8 test2 tab_tests;
9 BEGIN
10
11 select * bulk collect into test2 from table(test1) order by 1;
12 end;
13 end;
14 /
Warning: Package Body created with compilation errors.
SQL> show err
Errors for PACKAGE BODY PKG1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
11/1 PL/SQL: SQL Statement ignored
11/39 PL/SQL: ORA-22905: cannot access rows from a non-nested table
item
11/45 PLS-00642: local collection types not allowed in SQL statements
SQL> create or replace package body pkg1
2 as
3 PROCEDURE report (
4 i_cid IN NUMBER
5 )
6 IS
7 test1 scott.tab_tests := scott.tab_tests();
8 test2 scott.tab_tests;
9 BEGIN
10
11 select * bulk collect into test2 from table(test1) order by 1;
12 end;
13 end;
14 /
Package body created.
SQL>
SY.
|
|
|
|
|
|
| Re: sorting a nested table/collection [message #530975 is a reply to message #530973] |
Fri, 11 November 2011 11:16   |
shijumic
Messages: 31 Registered: May 2010
|
Member |
|
|
One more question - How can I get the elements in a collection in the descending order? I tried the following but it didn't work.
-- I have the data in test2 in a sorted order
for i in test2.LAST .. test2.FIRST
LOOP
DBMS_OUTPUT.PUT_LINE(test2(i));
END LOOP;
|
|
|
|
|
|
|
|
| Re: sorting a nested table/collection [message #531035 is a reply to message #531022] |
Sat, 12 November 2011 01:00   |
 |
Michel Cadot
Messages: 68773 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
When looping on a collection, it is better to do the following:
i := test.FISRT
LOOP
EXIT WHEN i IS NULL;
<do something>
i := test.NEXT(i);
END LOOP;
or
i := test.FISRT
WHILE i IS NOT NULL LOOP
<do something>
i := test.NEXT(i);
END LOOP;
Regards
Michel
[Updated on: Sat, 12 November 2011 01:01] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|