Home » SQL & PL/SQL » SQL & PL/SQL » PL/SQL cursor resultset for multiple cursor Urgent PLZ....
icon9.gif  PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #128641] Wed, 20 July 2005 00:07 Go to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Hi ,

i have come accross unique situation in pl/sql

i have got to generate statistics using ids from id table
and for each id i have to use cursor resultset to collect stattistics (e.g count in select statement) and then want to insert into table.

so basically i want to open cursor1 with first value then

open another cursor with all the rows (temporary table)

then
select count(column in cursor2)into r1 from cursor2 resultset where id in cursor2 = cursor1 id;
select count(column in cursor2) into r2from cursor2 result set....
....

r := r1||','||r2;

insert result into ddd(r)

here i have done something but no luck..any help would be highly appreciated.

=====
create or replace package stats_pkg
as

CURSOR jur_cur IS SELECT j_id,j_label from juri;
TYPE JuriCur IS REF CURSOR RETURN jur_cur%rowtype;

procedure pro_load_app(JuriCursor out JuriCur);
procedure pro_calculate_app_stats(JuriCursor in JuriCur);
l_sep varchar2(1) := ',';

end online_stats_pkg;
/


create or replace package body stats_pkg as

procedure pro_load_app(JuriCursor out JuriCur) is

begin
open JuriCursor for

SELECT j_id,j_label from juri;

end pro_load_app;

procedure pro_calculate_app_stats(JuriCursor in JuriCur) is

CURSOR app_cur is
SELECT app.S_CODE_NUM,
app.APP_DEF_J_ID,
NVL(inc.CURRENT_J_ID, app.APP_DEF_J_ID)
FROM appliance app ,incident inc where app.inc_id = inc.inc_id(+);

j_rec JuriCursor%rowtype;
st varchar2(2000);
adj varchar2(2000);
cj varchar2(2000);
r1 varchar2(100);
r2 varchar2(100);
r5 varchar2(100);
r6 varchar2(100);
ra varchar2(100);
l_start number;

begin

FETCH JuriCursor into j_rec;
while JuriCursor%FOUND loop

open app_cur;

FETCH app_cur into st,adj,cj; [ can i store resultsets in some table and query that table in below select???]
while app_cur%FOUND loop

l_start := dbms_utility.get_time;

execute immediate 'select count(st) from [??want to use cur_curcursor result set????] where adj = j_rec.j_id' into r1;

execute immediate 'select count(st) from [??want to use cur_curcursor result set????] where st in (1,2,3,4,5,6,7) and cj = j_rec.j_id' into r2;

execute immediate 'select count(st) from [??want to use cur_curcursor result set????] where st in (8,9,10,11,12) and adji = j_rec.j_id' into r5;

r6 := r2 * 100 / r2+r5;

commit;

ra := r1 || l_sep||r2||l_sep||l_sep||r5||l_sep||r6;
execute immediate 'insert into ddd values(ra)';


end loop;
end loop;

end ;


end stats_pkg;
/
======
Thanks


[Updated on: Wed, 20 July 2005 05:31]

Report message to a moderator

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #128741 is a reply to message #128641] Wed, 20 July 2005 06:46 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
See here:

http://download-west.oracle.com/docs/cd/B14117_01/appdev.101/b10807/12_tune.htm#sthref1348

,topic is "Using Pipelined Table Functions for Transformations".

Look at the example how to transform REF CURSOR into resultset
using pipelined table function.

Rgds.
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #128833 is a reply to message #128741] Wed, 20 July 2005 19:10 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Thanks Dimitry

the problem is i need get data from cursor 1 (suppose id) and for each id i need to collect statistics from another cursor result sets..so dont know how TABLE PIPELINE can help me.
icon5.gif  Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #128939 is a reply to message #128741] Thu, 21 July 2005 05:52 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
DROP TYPE nt_st;
/

DROP TYPE nt_adj;
/

DROP TYPE nt_cj;
/

drop package online_pkg
/


CREATE TYPE nt_st AS TABLE OF VARCHAR2(2000);
/

CREATE TYPE nt_adj AS TABLE OF number(10);
/

CREATE TYPE nt_cj AS TABLE OF number(10);
/

create or replace package online_pkg
as

TYPE JuriCur IS REF CURSOR;

procedure pro_load_app(JuriCursor out sys_refcursor);
procedure pro_cal_app(jcur in sys_refcursor);
end online_pkg;
/


create or replace package body online_pkg as

procedure pro_load_app(JuriCursor out sys_refcursor) is
TYPE juri_t IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
j_array juri_t;

TYPE rc_c IS REF CURSOR;
l_c1 rc_c;
l_v1 nt_st := nt_st(NULL);
l_v2 nt_adj := nt_adj(NULL);
l_v3 nt_cj := nt_cj(NULL);

cnt number;

begin

FETCH jcur BULK COLLECT INTO j_array;

FOR i IN 1 .. j_array.count
LOOP
OPEN l_c1 FOR SELECT app.ST_NUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+);

LOOP
FETCH l_c1 INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;

END LOOP;

CLOSE l_c1;
l_v1.TRIM;
l_v2.TRIM;
l_v3.TRIM;

select count(*) into cnt from TABLE(CAST(l_v1 as nt_st));

insert into ddd values(cnt);


END LOOP;

end;



end online_pkg;
/
SQL >DECLARE
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR SELECT j_id,j_lb from juris;
online_pkg.pro_cal_app(rc);
end;

show errors;

ERROR at line 1:
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at "ONLINE_PKG", line 28
ORA-06512: at line 5


=====================

Any idea/suggestion on this.

Thanks

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #128960 is a reply to message #128939] Thu, 21 July 2005 06:50 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
These are collections:

l_v1 nt_st := nt_st(NULL);
l_v2 nt_adj := nt_adj(NULL);
l_v3 nt_cj := nt_cj(NULL);


Then FETCH l_c1 INTO l_v1,l_v2,l_v3; is not allowed.

Instead of

Quote:


LOOP
FETCH l_c1 INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;

END LOOP;



has to be

FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;

Rgds.
icon9.gif  Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129076 is a reply to message #128960] Thu, 21 July 2005 18:46 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
I have changed BULK COLLECT for collections

FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;

but still the same error

ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129183 is a reply to message #129076] Fri, 22 July 2005 06:41 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Check carefuly the correspondence of column types returning by ref cursor and types of collections.

Rgds.
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129374 is a reply to message #129183] Sun, 24 July 2005 21:22 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dimitry

I have done following to read records from the collection table

rocedure pro_cal_app(jcur in sys_refcursor) is

TYPE juri_t IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
j_array juri_t;

cursor l_c1 is SELECT app.ST_NUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+);

l_v1 nt_st := nt_st(NULL);
l_v2 nt_adj:= nt_adj(NULL);
l_v3 nt_cj := nt_cj(NULL);

cnt number;
cnt1 number;
ct varchar2(2000) default null;
l_sep varchar2(1) default null;

begin

FETCH jcur BULK COLLECT INTO j_array;

FOR i IN 1 .. j_array.count
LOOP
open l_c1;
LOOP
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;
END LOOP;
for m in 1 .. l_v2.count loop

select count(*) into cnt from TABLE(CAST(l_v2 as nt_adj)) where l_v2(m) = j_array(i); ???? - i want to count for each id in j_array for matching ids in l_v2????

l_sep := ',';
ct := cnt||l_sep||cnt;

insert into ddd values(ct);
commit;

end loop;
end loop;

end;

Suggestions....
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129416 is a reply to message #129374] Mon, 25 July 2005 03:30 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
SQL> select * from t1;

       ID1
----------
         1
         2
         3
         4

SQL> select * from t2;

       ID2
----------
         1
         1
         1
         1
         1
         2
         2
         2
         3
         3
         3
         3
         3
         3
         3
         3
         3
         3

18 rows selected.

SQL> declare
  2   lv_2 nt_adj := nt_adj();
  3   cnt number;
  4  begin
  5   select * bulk collect into lv_2 from t2;
  6   for v in (select id1 from t1) loop
  7     select count(1)
  8     into cnt
  9     from table(cast(lv_2 as nt_adj)) a
 10     where a.column_value = v.id1;
 11     dbms_output.put_line('Number of values ' || v.id1 || ' is ' || cnt);
 12   end loop;
 13  end;
 14  /
Number of values 1 is 5
Number of values 2 is 3
Number of values 3 is 10
Number of values 4 is 0

PL/SQL procedure successfully completed.


Rgds.
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129452 is a reply to message #129416] Mon, 25 July 2005 05:53 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
dmitry, thanks for the solution , the only problem remain is

i have got sys cursor ========>
create or replace package online_pkg
as

TYPE JuriCur IS REF CURSOR;

procedure pro_load_app(JuriCursor out sys_refcursor);
procedure pro_cal_app(jcur in sys_refcursor);
end online_pkg;
/


create or replace package body online_pkg as

procedure pro_load_app(JuriCursor out sys_refcursor) is

which i am taking as input
, now in your provided solution,

declare
2 lv_2 nt_adj := nt_adj();
3 cnt number;
4 begin
5 select * bulk collect into lv_2 from t2

in my case ,my lv_2 comes fro sys_refcursor,so i can define it

FETCH jcur BULK COLLECT INTO j_array; <----- l_v2 in yr case

for v in (SELECT app.ST_NUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+)) loop

select count(*) into c1 from table(cast(lv2????) k where
v.app_def_jid = k.jid;

select count(*) into c2 from table(cast(lv2???) k where
v.st_num in (1,2,3) and v.app_def_jid = k.id;

ct := c1 || ',' || c2;
insert into dd values(ct);

end loop;
end;


================

Thanks in advance.




Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129490 is a reply to message #129452] Mon, 25 July 2005 08:07 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Do you mean to say you don't know in advance the type and number of columns which ref cursor returns to you ? If you know it
in advance there is not any remarkable difference between
my example and your case.

Rgds.

[Updated on: Mon, 25 July 2005 08:07]

Report message to a moderator

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129594 is a reply to message #129490] Mon, 25 July 2005 21:38 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Thanks Dmitry, i have done following changes

select jid,jlabel from juris;
1 SYS
2 AK
71 C1
81 C2
91 C3
101 F1
111 F2
121 F3
131 P1
141 P2
151 P3
161 A1
171 A2
181 A3
201 AC
211 T1
441 TEST

17 rows selected.
========================package ,body============

DROP TYPE nt_jid;

DROP TYPE nt_jlb;

drop package online_pkg;

CREATE TYPE nt_jid AS TABLE OF number;
/

CREATE TYPE nt_jlb AS TABLE OF VARCHAR2(10);
/

create or replace package online_pkg

as

procedure pro_load_app(JuriCursor out sys_refcursor);

procedure pro_cal_app(jcur in sys_refcursor);

end online_pkg;

/

create or replace package body online_pkg as

procedure pro_load_app(JuriCursor out sys_refcursor) is
begin
open JuriCursor for

SELECT jid,jlb from juris;

end pro_load_app;

procedure pro_cal_app(jcur in sys_refcursor) is

l_jid nt_jid := nt_jid(null);
l_jlb nt_jlb := nt_jlb(null);

ct varchar2(2000);
cnt1 number;
cnt2 number;

begin

FETCH jcur BULK COLLECT INTO l_jid,l_jlb;

FOR v in (SELECT app.ST_NUM,app.APP_DEF_JID,NVL(inc.CUR_jID, app.APP_DEF_jID) cur FROM appli app,incid inc where app.inc_id = inc.inc_id(+))

loop

select count(*) into cnt1 from TABLE(CAST(l_jid as nt_jid)) a where v.APP_DEF_JID = a.column_value;

ct := cnt1 || ',' || cnt1 ;

insert into ddd values(ct);
commit;

END LOOP;

end;

end online_pkg;

SQL>DECLARE
rc SYS_REFCURSOR;
begin
open rc for SELECT jid,jlb from juris;
online_pkg.pro_cal_app(rc);
end;
PL/SQL procedure successfully completed.

SQL> select count(*) from ddd;

COUNT(*)
----------
424

SQL> desc ddd;
Name Null? Type
----------------------------------------- -------- ------------
ID VARCHAR2(2000)

which is incorrect, it should be 17 right and values were not correct

dont know where things go wrong?

Any mistake do you see in above

Thanks...
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129620 is a reply to message #129594] Tue, 26 July 2005 00:21 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry
i have corrected the previous mentioned problem, now one part remained is

i have got 3 collections set
cursor bulk collect into l_v1,l_v2,l_v3;

and i want to do query to count


for i in 1 .. j_array.count ---- for each id in j_array
loop

select count(*) into cnt from table(cast(l_v2 as nt_adj)) a where a.column_value = j_array(i) order by j_array(i); --OK

select count(*) from table(cast(l_v1 as aa)) a, table(cast(l_v2)) b where b.column_value = j_array(i) and a.column_value in (1,2,3,4,5); ---- Problem ????

so i need to find out total count in l_v1 where id associated with l_v1 (that is in l_v2 collection) is equal to j_array(i);

output should be look this
APP_DEF_JID
--------------
1 ,1
2 ,291
71 ,2
81 ,0
91 ,0
101 ,0
111 ,0
121 ,0
131 ,0
141 ,0
151 ,1
161 ,0
171 ,0
181 ,0
201 ,0
211 ,1
441 ,0

17 rows selected.

Current Output shows something like this (291+1+2+1+1 = 296)
SQL> select * from dd;

ID
----
2,296

417,296

3,296
0,296

0,296

0,296
0,296
0,296
0,296
0,296
1,296
0,296
0,296
0,296
0,296

17 rows selected.


any suggestions..
Thanks

Package ----

l_v1 nt_st := nt_st(NULL);
l_v2 nt_adj:= nt_adj(NULL);
l_v3 nt_cj := nt_cj(NULL);

cnt number(10);
cnt1 number;
ct varchar2(2000) default null;
l_sep varchar2(1) default null;

begin
create or replace package body online_pkg as

procedure pro_load_app(JuriCursor out sys_refcursor) is

begin
open JuriCursor for

SELECT jurisdiction_id,jurisdiction_label from jurisdiction;

end pro_load_app;

procedure pro_cal_app(jcur in sys_refcursor) is

TYPE juri_t IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
j_array juri_t;

cursor l_c1 is SELECT app.STNUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_J
URISDICTION_ID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+);

FETCH jcur BULK COLLECT INTO j_array;

FOR i IN 1 .. j_array.count
LOOP
open l_c1;
LOOP
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;
END LOOP;

CLOSE l_c1;


select count(*) into cnt from table(cast(l_v2 as nt_adj)) a where a.column_value = j_array(i) order by j_array(i); --OK

select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i) ; -- Problem???

ct := cnt||l_sep||cnt1;
l_sep := ',';
insert into dd values(ct,to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS'));

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129647 is a reply to message #129416] Tue, 26 July 2005 02:45 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry
i have also tried this similar to the solution provided by you but no luck

============package================
drop type nt_jid;
drop type nt_jlb;

drop package online_pkg;

CREATE TYPE nt_jlb AS TABLE OF VARCHAR2(10);
/
CREATE TYPE nt_jid AS TABLE OF number;
/


create or replace package online_pkg
as

TYPE JuriCur IS REF CURSOR;

procedure pro_load_app(JuriCursor out sys_refcursor);
procedure pro_cal_app(jcur in sys_refcursor);
end online_pkg;
/


create or replace package body online_pkg as

procedure pro_cal_app(jcur in sys_refcursor) is
begin
open JuriCursor for

SELECT jid,jlb from juris;

end pro_load_app;

procedure pro_cal_app(jcur in sys_refcursor) is


j_id nt_jid;
j_lb nt_jlb;

cnt number(10);
cnt1 number;
ct varchar2(2000) default null;
l_sep varchar2(1) default null;

begin

FETCH jcur BULK COLLECT INTO j_id,j_lb;

for r in (SELECT app.ST_NUM,app.APP_DEF_JID p,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+))

loop
select count(1) into cnt from table(cast(j_id as nt_jid)) a where a.column_value = r.p ;

ct := cnt ||','||cnt;

insert into ddd values(ct);
commit;
end loop;

end;

end online_pkg;
/
show errors;

Output
...
...
1,1
1,1
1,1
1,1
1,1
1,1

424 rows selected. Should be only 17 rows.
output should be look this
JID
--------------
1 ,1
2 ,291
71 ,2
81 ,0
91 ,0
101 ,0
111 ,0
....
Suggestions??

Thanks



Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129668 is a reply to message #129647] Tue, 26 July 2005 04:03 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Sam,

the only thing I can suggest is to check your code and result sets carefully - I don't have a chance to do it bacause of the lack of tables/data.

Shooting a glance at your code I'm quite confused with:

FETCH jcur BULK COLLECT INTO j_id,j_lb;

for r in (SELECT app.ST_NUM,app.APP_DEF_JID p,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+))

loop
select count(1) into cnt from table(cast(j_id as nt_jid)) a where a.column_value = r.p ;

ct := cnt ||','||cnt;

insert into ddd values(ct);

commit;
end loop;

Is it your aim ? Based on your example I would say of

ct := r.p ||','||cnt;

Just use dbms_output to trace values you use,
(and also don't forget cleanup ddd table before
new inserts).

Rgds.
icon9.gif  Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129714 is a reply to message #129668] Tue, 26 July 2005 06:42 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
First i would like to appreciate yr suggestions/comments to help me out.

I am in bad situations.

i have got 3 tables

juris (jid number,jlb varchar2(10));

1,sys
2,ab
3,bc
4,cc

appli (app_def_jid numer,st_num number);
1,12
1,13
2,12
3,12
2,12
3,13
4,14
2,14
5,12
5,12
5,13

inci (cur_jid number,inc_stat varchar2(10);

1,ava
2,bzy
3,ava
4,ava
5,bzy

till now i have done
1>load jid,jlb using ref cursor
2>using cursor collected data into collection TABLE
3>use jid for loop (j_array in my code) for each jid
4>collected data from cursor into l_v1,l_v2,l_v3

count(app_def_jid) in cnt from table(cast(l_v2))a where
a.column_value = j_array(i); OK

count(st_num) in cnt1 from table(cast(l_v1)) a ,table(cast(l_v2)) b where a.column_value in (1,2,3,4) and b.column_value = j_array(i); Problem???

....
..

ct := cnt1 ||',' ||cnt2 .....;
insert into ddd values(jlb,sysdate,ct);
end loop;
end;


========complete pacakge=============

create or replace package body online_pkg as

procedure pro_load_app(JuriCursor out sys_refcursor) is

begin
open JuriCursor for

SELECT jurisdiction_id,jurisdiction_label from jurisdiction;

end pro_load_app;

procedure pro_cal_app(jcur in sys_refcursor) is

TYPE juri_t IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
j_array juri_t;

cursor l_c1 is SELECT app.STNUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_J
URISDICTION_ID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+);

l_v1 nt_st := nt_st(NULL);
l_v2 nt_adj:= nt_adj(NULL);
l_v3 nt_cj := nt_cj(NULL);

cnt number(10);
cnt1 number;
ct varchar2(2000) default null;
l_sep varchar2(1) default null;

begin

FETCH jcur BULK COLLECT INTO j_array;

FOR i IN 1 .. j_array.count
LOOP
open l_c1;
LOOP
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;
END LOOP;

CLOSE l_c1;


select count(*) into cnt from table(cast(l_v2 as nt_adj)) a where a.column_value = j_array(i) order by j_array(i); --OK

select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i) ; -- Problem???

ct := cnt||l_sep||cnt1;
l_sep := ',';
insert into dd values(ct,to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS'));

end loop
end;

==================END PACKAGE===============
OUTPUT
SQL> select * from dd;

count1,count2,......
----
2,296
417,296
3,296
0,296
0,296
0,296
0,296
0,296
0,296
0,296
1,296
0,296
0,296
0,296
0,296

17 rows selected.

===========================
OUTPUT Supposed to be like this
=============================
count1,count2
--------------
1 ,1
2 ,291
71 ,2
81 ,0
91 ,0
101 ,0
111 ,0
121 ,0
131 ,0
141 ,0
151 ,1
161 ,0
171 ,0
181 ,0
201 ,0
211 ,1
441 ,0

17 rows selected.
===========================================

Any kind of comments,suggestions highly appreciated.
as i have limited time complete this and i am in really really bad shape, this is the first time i have come accross such pl/sql challenging tasks and i am working harder and harder, may be with your little help i could achieve this...

thanks
Sam
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129727 is a reply to message #129714] Tue, 26 July 2005 07:45 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Sam,

if you are looking for help from other people you should
provide the exact information of your code /structures.

You are talking of

SELECT app.ST_NUM,app.APP_DEF_JID p,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+)

statement as an example, but I don't see inc_id columns
in your APPLI and INCID tables.

What about it ?


Next,

Quote:


FOR i IN 1 .. j_array.count LOOP
open l_c1;
LOOP
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;
l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;
END LOOP;
CLOSE l_c1;


select count(*) into cnt from table(cast(l_v2 as nt_adj)) a where a.column_value = j_array(i) order by j_array(i); --OK

select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i) ; -- Problem???

ct := cnt||l_sep||cnt1;
l_sep := ',';
insert into dd values(ct,to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS'));

end loop



Sorry, it looks like a mess.

You don't need to open cursor each time in loop - it doesn't have
any parameter and can be fetched once. LOOP ... END LOOP; is nonsense here (you don't use LIMIT clause).

l_v1.EXTEND;
l_v2.EXTEND;
l_v3.EXTEND;
is a great mistake !

Can be something like

Quote:


begin

FETCH jcur BULK COLLECT INTO j_array;

open l_c1;
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
CLOSE l_c1;

FOR i IN 1 .. j_array.count

select count(*) into cnt from table(cast(l_v2 as nt_adj)) a where a.column_value = j_array(i);

...

ct := cnt||l_sep||cnt1;
l_sep := ',';
insert into dd values(ct,to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS'));

end loop;
end;



And at last what are you trying to do
here:

Quote:


select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i);



?

It is simply the cartesian join and = (number
of rows in l_vt which are 1,2,3,4 or 5) * (number
of rows in l_v2 where value = j_array(i));

Rgds.

[Updated on: Tue, 26 July 2005 07:46]

Report message to a moderator

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129735 is a reply to message #129727] Tue, 26 July 2005 08:08 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry, accept my apology, was in bit rush..

In below cursor

cursor l_c1 is
SELECT app.ST_NUM,app.APP_DEF_JID p,NVL(inc.CUR_JID, app.APP_DEF_JID) FROM appli app,incid inc where app.inc_id = inc.inc_id(+)

output of cursor query :
1 13 13
3 13 13
2 2 2
5 2 2
1 1 1
4 3 3


inc_id can be 1,2,3,4,5,6....

so i am collecting 3 values into l_c1 cursor,

then using bulk collect

FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;

nightmare starts from here.

because l_v1 --st_num
l_v2 --app_def_jid
l_v3 --(inc.cur_jid,app.app_def_jid)

and jcur has select jid,jlb from juris;

select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i);

In above query i want to count st_num whose corresponding id in l_v2 ie a.column_value = j_array(i);

so in above query i can assign b.column_value = j_array(i);
and then for count(st_num) for associated b.column_value

Dmitry this may help to you comments/suggestions on query
thanks
sam
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129739 is a reply to message #129727] Tue, 26 July 2005 08:15 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry

i need to write down below query such a way that i can get
count(st_num) whose corresponding id in l_v2 then
each correspoding id in l_v2 match with j_array(i) gives final count.

i can write down above req using below query


SELECT count(app.ST_NUM)
FROM appli app,incid inc where app.inc_id = inc.inc_id(+) and app.st_num in (1,2,3,4,5) and
app.app_def_jid = '71'; ( which comes from j_array in for loop)


This is i am trying to achieve.

Regards
sam
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129742 is a reply to message #129739] Tue, 26 July 2005 08:36 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
But it this case I'm not able to understand why don't you use
something like:

FETCH jcur BULK COLLECT INTO j_array;
CLOSE jcur;

FOR j IN 1..j_array.count LOOP

SELECT count(app.ST_NUM) INTO <<counter>>
FROM appli app,incid inc where app.inc_id = inc.inc_id(+) and app.st_num in (1,2,3,4,5) and
app.app_def_jid = j_array(j);

END LOOP;

?

Rgds.
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129829 is a reply to message #129742] Tue, 26 July 2005 18:40 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry,

i could have done that, but my package runs at every 15 mins, then those tables will have more and more data, then would absolutely affect performance, because i have got 32 different kinds of queries with different calculations will run in single for loop for each jid in j_array.

That's why i am storing those values from cursor to the collection tables and query from there.
So if you could suggest any alternate to this query using collection table would be highly regarded.

FETCH jcur BULK COLLECT INTO j_array;
CLOSE jcur;

FOR j IN 1..j_array.count LOOP

[QUERY1]
SELECT count(app.ST_NUM) INTO <<counter>>
FROM appli app,incid inc where app.inc_id = inc.inc_id(+) and app.st_num in (1,2,3,4,5) and app.app_def_jid = j_array(j);

END LOOP;
============================
FETCH l_c1 BULK COLLECT INTO l_v1,l_v2,l_v3;
EXIT WHEN l_c1%NOTFOUND;
CLOSE l_c1;


select count(*) into cnt1 from table (cast(l_v1 as nt_st)) a , table(cast(l_v2 as nt_adj)) b where a.column_value in (1,2,3,4,5) and b.column_value = j_array(i) ;--- Incorrect,Probelm??? not getting the same result as query described above [QUERY1]

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129860 is a reply to message #129829] Wed, 27 July 2005 00:46 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
I doubt the such using of collection can help you with performance.

OK, my suggestion is to use an object type and table of objects.
The problem is you are trying to simulate query where you work
with tuple using two unrelated collections.

I hope this example can help:

SQL> create type my_obj is object( empno number, sal number, deptno number);
  2  /

Type created.

SQL> create type my_obj_tab is table of my_obj;
  2  /

Type created.

SQL> declare
  2  
  3   my_tab my_obj_tab := my_obj_tab();
  4  
  5   cnt number;
  6   cnt1 number;
  7  
  8  begin
  9  
 10   --Let's fill the object collection using object default constructor
 11  
 12   select my_obj(empno,sal,deptno) 
 13   bulk collect into my_tab
 14   from emp;
 15  
 16   for v in (select deptno from dept) loop
 17    
 18     select count(1) into cnt from 
 19     table(cast(my_tab as my_obj_tab)) a
 20     where a.deptno = v.deptno;
 21  
 22     select count(1) into cnt1 from   
 23     table(cast(my_tab as my_obj_tab)) a
 24     where a.deptno = v.deptno and a.sal > 1000;
 25  
 26     dbms_output.put_line('Number of employee in department ' 
 27     || v.deptno || ' is ' || cnt);
 28  
 29     dbms_output.put_line('Number of employee in department ' 
 30     || v.deptno || ' with salary > $1000 is ' || cnt);
 31  
 32   end loop;
 33  
 34  end;
 35  /
Number of employee in department 10 is 3
Number of employee in department 10 with salary > $1000 is 3
Number of employee in department 20 is 5
Number of employee in department 20 with salary > $1000 is 5
Number of employee in department 30 is 6
Number of employee in department 30 with salary > $1000 is 6
Number of employee in department 40 is 0
Number of employee in department 40 with salary > $1000 is 0
Number of employee in department 50 is 0
Number of employee in department 50 with salary > $1000 is 0
Number of employee in department 60 is 0
Number of employee in department 60 with salary > $1000 is 0

PL/SQL procedure successfully completed.

Rgds.
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129895 is a reply to message #129860] Wed, 27 July 2005 03:52 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry

Two things i didnt get it in your given example,

First> on Line 14

select my_obj(empno,sal,deptno)
13 bulk collect into my_tab
14 from emp;

Line 14 : from <table name>

In my case ,
object is made of 2 tables eg. appli a and incid i

my_obejct(a.st_num,a.app_def_jid,nvl(i.cur_jid,a.app_def_jid)

select my_obejct(st_num,app_def_jid,nvl(i.cur_jid,a.app_def_jid) bulk collect into my_tab
from ???

>Second thing on line 18
select count(1) into cnt
what is the value used in count(?)

I think this will sort it out my problem.

Regards
Sam

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129907 is a reply to message #129895] Wed, 27 July 2005 04:28 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Sam,

never mind FROM clause - emp in my example is just
for simplicity. Selection list has a meaning only.
I could use "from emp, dept where ...." - mo matter.

So for example:

SELECT my_obj(app.ST_NUM,app.APP_DEF_JID,NVL(inc.CUR_JID, app.APP_DEF_JID))
bulk collect into <<nested table of objects>>
FROM appli app,incid inc where app.inc_id = inc.inc_id(+)

is OK.


count(1), count(2), count('a'),...count(*) mean the same -
number of records in select.

Rgds.

[Updated on: Wed, 27 July 2005 04:31]

Report message to a moderator

icon14.gif  Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129908 is a reply to message #129907] Wed, 27 July 2005 04:41 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Thanks Mate. Got it.

Challenge 1 (start)
Challenge 1 (complete)
Now Challenge 2 (start)
...
...
icon5.gif  Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129946 is a reply to message #129908] Wed, 27 July 2005 07:10 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Hey Dmitry

is there any way to replace outer join with normal join or any other way,

select a.aa,a.bb
from a, b
where a.id = b.id(+);

bcoz outer join affects performance and trying to improve the performance.

Thanks
Sam
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129948 is a reply to message #129946] Wed, 27 July 2005 07:21 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
It doesn't make sense at all.
The performance is defined by other factors, major of them are proper indexes and up-to-date statistics.

Technically you can
SQL> select d.deptno, e.ename from emp e, dept d
  2  where e.deptno(+) = d.deptno order by 1;

    DEPTNO ENAME
---------- ----------
        10 CLARK
        10 KING
        10 MILLER
        20 SMITH
        20 ADAMS
        20 FORD
        20 SCOTT
        20 JONES
        30 ALLEN
        30 BLAKE
        30 MARTIN

    DEPTNO ENAME
---------- ----------
        30 JAMES
        30 TURNER
        30 WARD
        40
        50
        60

17 rows selected.

SQL> select d.deptno, e.ename from emp e, dept d
  2  where e.deptno = d.deptno 
  3  union all
  4  select deptno, null from dept d where not exists
  5  (select 1 from emp e where d.deptno = e.deptno)
  6  order by 1;

    DEPTNO ENAME
---------- ----------
        10 CLARK
        10 KING
        10 MILLER
        20 SMITH
        20 ADAMS
        20 FORD
        20 SCOTT
        20 JONES
        30 ALLEN
        30 BLAKE
        30 MARTIN

    DEPTNO ENAME
---------- ----------
        30 JAMES
        30 TURNER
        30 WARD
        40
        50
        60

17 rows selected.


but it can't help you.

Rgds.

[Updated on: Wed, 27 July 2005 07:22]

Report message to a moderator

Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129966 is a reply to message #129452] Wed, 27 July 2005 08:22 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Thanks Dmitry

i m using distinct in select statement

and i have got suppose 3 records like this

id time
1 19.24.11:11
1 19.23.11:12
3 19.22.11:12

select distinct(id) from d ;

i am getting

1
3

so shall i assume that it will have the latest record
1 19.24.11:11
3 19.22.11:12

Thanks
Sam
Re: PL/SQL cursor resultset for multiple cursor Urgent PLZ.... [message #129970 is a reply to message #129966] Wed, 27 July 2005 08:41 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
No. DISTINCT affects all tuple columns in selection list and
it is not related to ordering or aggregation.
For getting the latest/earliest rows you should use max/min
and group by:

SQL> select distinct * from (
  2  select 1 a, '19.24.11:11' b from dual
  3  union all
  4  select 1, '19.23.11:12' from dual
  5  union all
  6  select 3, '19.22.11:12' from dual
  7  )
  8  /

             A B
-------------- -----------
             1 19.23.11:12
             1 19.24.11:11
             3 19.22.11:12

SQL> select id, max(b) from (
  2  select 1 id, '19.24.11:11' b from dual
  3  union all
  4  select 1, '19.23.11:12' from dual
  5  union all
  6  select 3, '19.22.11:12' from dual
  7  ) c
  8  group by id
  9  /

        ID MAX(B)
---------- -----------
         1 19.24.11:11
         3 19.22.11:12


Rgds.
Re: PL/SQL DML on Collections Thanks [message #130146 is a reply to message #129970] Thu, 28 July 2005 07:46 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Thanks Dmitry

can it be possible to do following things

define type mytype is obejct(a,b,c)
type mytt is table of mytype;

create package
procedure test(j sys_refcursor)

my_tab mytt := mytt();

begin

select mytab(a,max(b),c) bulk collect into my_tab from table group by a;

max(b) --- is above statement is valid in pl/sql..


Thanks
Re: PL/SQL DML on Collections Thanks [message #130152 is a reply to message #130146] Thu, 28 July 2005 08:08 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
>>select mytab(a,max(b),c) bulk collect into my_tab from table >>group by a;

select mytab(a,max(b),c) bulk collect into my_tab from table group by a, c;

Yes, easy - just try it.

SQL> create type my_obj is object(deptno number, job varchar2(10), sal nu
  2  /

Type created.

SQL> create type my_obj_tab is table of my_obj;
  2  /

Type created.

SQL> select deptno, job, sal from emp;

    DEPTNO JOB              SAL
---------- --------- ----------
        20 CLERK           1000
        30 SALESMAN        1800
        30 SALESMAN        1450
        20 MANAGER         2975
        30 SALESMAN        1250
        30 MANAGER         2850
        10 MANAGER         2450
        20 ANALYST         3000
        10 PRESIDENT       5000
        30 SALESMAN        1500
        20 CLERK           1100

    DEPTNO JOB              SAL
---------- --------- ----------
        30 CLERK            950
        20 ANALYST         3000
        10 CLERK           1300

14 rows selected.

SQL> declare
  2   mytab my_obj_tab := my_obj_tab();
  3  begin
  4  
  5   select my_obj(deptno, job, max(sal))
  6   bulk collect into mytab
  7   from emp
  8   group by deptno, job;
  9  
 10   for i in 1..mytab.count loop
 11     dbms_output.put_line(mytab(i).deptno || ' ' 
 12     || mytab(i).job || ' ' || mytab(i).sal);
 13   end loop;
 14  
 15  end;
 16  /
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 CLERK 1100
20 ANALYST 3000
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 1800

PL/SQL procedure successfully completed.


Rgds.
Re: PL/SQL DML on Collections Thanks [message #130297 is a reply to message #130152] Fri, 29 July 2005 03:37 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry

i have defined following obj and type

create type fm_obj is object(INC_ID number,INC_TS date,LOG_TS date,INC_ST varchar2(10),LG_ENTRY varchar2(5),CURR_JID number);
/
create type fm_obj_tab is table of fm_obj;

Package

package Body
fm_tab fm_obj_tab := fm_obj_tab();

procedure
begin
select fm_obj(INC_ID,INC_TS,max(LOG_TS),INC_ST,LG_ENTRY,CURR_JID) bulk collect into fim_tab from fim f,incid i ,juris j where f.lg_entry = mob and i.inc_st = clos and f.inc_id = i.inc_id and j.jurisdiction_id = i.curr_jid(+)group by f.inc_id;

fetch jcur bulck collect into j_array;
for j 1 .. in j_array.count
loop


SELECT round(SUM(SUM(to_char(to_date('00:00:00','HH24:MI:SS') +( f.INC_TS - f.LOG_TS),'MI')))/60) into
imin from table(cast(fm_tab as fm_obj_tab)) f where f.LG_ENTRY= mob and f.INC_ST = clos and f.CURR_JID = j_array(j) group by f.INC_ID;


Got the following error
PL/SQL: ORA-02315: incorrect number of arguments for default
constructor

PL/SQL: SQL Statement ignored
201/60 PL/SQL: ORA-00904: "F"."INC_TS": invalid identifier

I have checked all the possible things but i cant see any problem in parameters defined in the constructor and object.

Thanks
Sam
Thanks

[Updated on: Fri, 29 July 2005 04:01]

Report message to a moderator

Re: PL/SQL DML on Collections Thanks [message #130319 is a reply to message #130297] Fri, 29 July 2005 06:10 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Sam,

To be franky I think you can eaisily to find the reasons of such errors yourself, just read carefully
"PL/SQL: ORA-02315: incorrect number of arguments for default
constructor
" and compare:

fm_obj(
INC_ID,
INC_TS,
max(LOG_TS),
INC_ST,
LG_ENTRY,CURR_JID
)

and

object(
INC_ID number,
INC_TS date,
LOG_TS date,
INC_ST varchar2(10),
LG_ENTRY varchar2(5),
CURR_JID number
)


Your object has 6 parameters in default constructor
but you are trying to create it using 5.

Rgds.
Re: PL/SQL DML on Collections Thanks [message #130414 is a reply to message #130319] Fri, 29 July 2005 21:54 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Actually in your reply curr_jid and lg_entry were in the same line that's why it looks five instead of six args.


create type fm_obj is object(INC_ID number,INC_TS date,LOG_TS date,INC_ST varchar2(10),LG_ENTRY varchar2(5),CURR_JID number);
/
create type fm_obj_tab is table of fm_obj;

====object==
INC_ID number,
INC_TS date,
LOG_TS date,
INC_ST varchar2(10),
LG_ENTRY varchar2(5),
CURR_JID number;

=====constructor=====

select fm_obj(INC_ID,INC_TS,max(LOG_TS),INC_ST,LG_ENTRY,CURR_JID) bulk collect into fim_tab from fim f,incid i ,juris j where f.lg_entry = mob and i.inc_st = clos and f.inc_id = i.inc_id and j.jurisdiction_id = i.curr_jid(+)group by f.inc_id;

=====fim_obj===============
(INC_ID,
INC_TS,
max(LOG_TS),
INC_ST,
LG_ENTRY,
CURR_JID)


fetch jcur bulck collect into j_array;

for j 1 .. in j_array.count
loop


SELECT round(SUM(SUM(to_char(to_date('00:00:00','HH24:MI:SS') +( f.INC_TS - f.LOG_TS),'MI')))/60) into
imin from table(cast(fm_tab as fm_obj_tab)) f where f.LG_ENTRY= mob and f.INC_ST = clos and f.CURR_JID = j_array(j) group by f.INC_ID;


Got the following error
PL/SQL: ORA-02315: incorrect number of arguments for default
constructor

PL/SQL: SQL Statement ignored
201/60 PL/SQL: ORA-00904: "F"."INC_TS": invalid identifier

Thanks
Sam

[Updated on: Fri, 29 July 2005 21:56]

Report message to a moderator

Re: PL/SQL DML on Collections Thanks [message #130421 is a reply to message #130414] Sat, 30 July 2005 02:01 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Oops, sorry... Embarassed I need more stiff coffee Laughing

OK, check your object argument types correspond to that in
select. It also can cause a problem.

The best way would be to post up-to-date tables structure -
then it can be checked.

Rgds.

[Updated on: Sat, 30 July 2005 02:02]

Report message to a moderator

icon9.gif  Re: PL/SQL DML on Collections Thanks [message #130475 is a reply to message #130421] Sun, 31 July 2005 02:12 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry

here is structure
incid(
INC_ID NUMBER(38,0) NOT NULL
INC_TS DATE NOT NULL
INC_ST VARCHAR2(10) NOT NULL
CURR_JID NUMBER(0,0) NOT NULL
);

fim
(LOG_TS DATE NOT NULL
LG_ENTRY VARCHAR2(10) NOT NULL
INC_ID NUMBER(38,0) );

===================================

create type inc_obj is object(INC_STAT varchar2(10),INC_CTS date,PTY number,CURR_JID number,INC_TS date); --Ok
/

create type inc_obj_tab is table of inc_obj; --- Ok

create type fm_obj is object(INC_ID number,INC_TS date,LOG_TS date,INC_ST varchar2(10),LG_ENTRY varchar2(10),CURR_JID number);
/
-- This type has problem in Select constructor????

create type fm_obj_tab is table of fm_obj;

====object==
INC_ID number,
INC_TS date,
LOG_TS date,
INC_ST varchar2(10),
LG_ENTRY varchar2(5),
CURR_JID number;

=====constructor=====

select inc_obj(INC_STAT,INC_CTS,PTY,CURR_JID,INC_CTS) bulk collect into inc_tab from incid i ,juris j where j.jid = inc.curr_jid(+); ----- works OK

select fm_obj(INC_ID,INC_TS,max(LOG_TS),INC_ST,LG_ENTRY,CURR_JID) bulk collect into fim_tab from fim f,incid i ,juris j where f.lg_entry = mob and i.inc_st = clos and f.inc_id = i.inc_id and j.jurisdiction_id = i.curr_jid(+)group by f.inc_id; ---- Problem ??? Dont knoww???

=====fim_obj===============
(INC_ID,
INC_TS,
max(LOG_TS),
INC_ST,
LG_ENTRY,
CURR_JID)


fetch jcur bulck collect into j_array;

for j 1 .. in j_array.count
loop


SELECT round(SUM(SUM(to_char(to_date('00:00:00','HH24:MI:SS') +( f.INC_TS - f.LOG_TS),'MI')))/60) into
imin from table(cast(fm_tab as fm_obj_tab)) f where f.LG_ENTRY= mob and f.INC_ST = clos and f.CURR_JID = j_array(j) group by f.INC_ID;


Got the following error
PL/SQL: ORA-02315: incorrect number of arguments for default
constructor

PL/SQL: SQL Statement ignored
201/60 PL/SQL: ORA-00904: "F"."INC_TS": invalid identifier


Thanks
Sam

[Updated on: Sun, 31 July 2005 02:36]

Report message to a moderator

Re: PL/SQL DML on Collections Thanks [message #130506 is a reply to message #130475] Sun, 31 July 2005 18:29 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
Your specific problem is unclear, but it looks like the way you are trying to do it is the hard way and probably not very efficient. I gather that you want to gather some counts based on certain conditions and insert those counts into another table. The following example uses various pieces of what you have provided. It uses simple for loop, insert, and select, without any dynamic sql or types or collecting. It uses the subquery factoring (with) clause to reduce the executions of the redundant subquery. If this is not enough to point you in the right direction, then post create table statements, insert statements for sample data, and the results that you want that correspond to that data. Do not just post descriptions of your table structure with abbreviated names, selects of sample data, and pieces of code and results, where the pieces don't match, as you have previously done. You should also test your create table and insert statements in another schema, before posting.

I am editing this and posting without formatting, because every time I tried to format in the usual manner, the code box ended up blank.

-- tables and data:
scott@ORA92> CREATE TABLE appli
2 (st_num NUMBER,
3 app_def_jid NUMBER,
4 inc_id NUMBER)
5 /

Table created.

scott@ORA92> INSERT ALL
2 INTO appli VALUES (1, 13, 1)
3 INTO appli VALUES (3, 13, 2)
4 INTO appli VALUES (2, 2, 3)
5 INTO appli VALUES (5, 2, 4)
6 INTO appli VALUES (1, 1, 5)
7 INTO appli VALUES (4, 3, 6)
8 SELECT * FROM DUAL
9 /

6 rows created.

scott@ORA92> CREATE TABLE incid
2 (inc_id NUMBER,
3 inc_stat VARCHAR2(10),
4 cur_jid NUMBER)
5 /

Table created.

scott@ORA92> INSERT ALL
2 INTO incid VALUES (1, 'ava', 13)
3 INTO incid VALUES (2, 'bzy', 13)
4 INTO incid VALUES (3, 'ava', 2)
5 INTO incid VALUES (4, 'ava', 2)
6 INTO incid VALUES (5, 'bzy', 1)
7 SELECT * FROM DUAL
8 /

5 rows created.

scott@ORA92> CREATE TABLE juris
2 (jid NUMBER,
3 jlb VARCHAR2(10))
4 /

Table created.

scott@ORA92> INSERT ALL
2 INTO juris VALUES (1, 'sys')
3 INTO juris VALUES (2, 'ab')
4 INTO juris VALUES (3, 'bc')
5 INTO juris VALUES (4, 'cc')
6 SELECT * FROM DUAL
7 /

4 rows created.

scott@ORA92> CREATE TABLE ddd
2 (id NUMBER,
3 counts VARCHAR2(20))
4 /

Table created.

-- package, execution, and results:
scott@ORA92> CREATE OR REPLACE PACKAGE online_pkg
2 AS
3 PROCEDURE pro_cal_app;
4 END online_pkg;
5 /

Package created.

scott@ORA92> SHOW ERRORS
No errors.
scott@ORA92> CREATE OR REPLACE PACKAGE BODY online_pkg
2 AS
3 PROCEDURE pro_cal_app
4 IS
5 BEGIN
6 FOR j_rec IN
7 (SELECT jid FROM juris)
8 LOOP
9 INSERT INTO ddd
10 WITH subquery AS
11 (SELECT appli.st_num,
12 appli.app_def_jid,
13 NVL (incid.cur_jid, appli.app_def_jid) cj
14 FROM appli, incid
15 WHERE appli.inc_id = incid.inc_id (+))
16 SELECT j_rec.jid, ct1.cnt || ',' || ct2.cnt || ',' ||ct3.cnt
17 FROM (SELECT COUNT(*) cnt
18 FROM subquery
19 WHERE app_def_jid = j_rec.jid) ct1,
20 (SELECT COUNT(*) cnt
21 FROM subquery
22 WHERE st_num IN (1,2,3,4,5,6,7)
23 AND app_def_jid = j_rec.jid) ct2,
24 (SELECT COUNT(*) cnt
25 FROM subquery
26 WHERE st_num IN (8,9,10,11,12)
27 AND app_def_jid = j_rec.jid) ct3;
28 END LOOP;
29 END pro_cal_app;
30 END online_pkg;
31 /

Package body created.

scott@ORA92> SHOW ERRORS
No errors.
scott@ORA92> EXECUTE online_pkg.pro_cal_app

PL/SQL procedure successfully completed.

scott@ORA92> SELECT * FROM ddd
2 /

ID COUNTS
---------- --------------------
1 1,1,0
2 2,2,0
3 1,1,0
4 0,0,0

scott@ORA92>

[Updated on: Sun, 31 July 2005 18:32]

Report message to a moderator

Re: PL/SQL DML on Collections Thanks [message #130514 is a reply to message #130475] Sun, 31 July 2005 19:00 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
For some strange reason the code tags don't seem to be working for me on this forum today. The following is another attempt to post the package code, using pre tags instead.

CREATE OR REPLACE PACKAGE online_pkg 
AS
  PROCEDURE pro_cal_app;
END online_pkg;
/
SHOW ERRORS
CREATE OR REPLACE PACKAGE BODY online_pkg 
AS
  PROCEDURE pro_cal_app
  IS
  BEGIN
    FOR j_rec IN
      (SELECT jid FROM juris)
    LOOP
      INSERT INTO ddd
      WITH   subquery AS
             (SELECT appli.st_num,
                     appli.app_def_jid,
                     NVL (incid.cur_jid, appli.app_def_jid) cj
              FROM   appli, incid 
              WHERE  appli.inc_id = incid.inc_id (+))
      SELECT j_rec.jid, ct1.cnt || ',' || ct2.cnt || ',' ||ct3.cnt
      FROM   (SELECT COUNT(*) cnt
              FROM   subquery
              WHERE  app_def_jid = j_rec.jid) ct1, 
             (SELECT COUNT(*) cnt
              FROM   subquery
              WHERE st_num IN (1,2,3,4,5,6,7) 
              AND   app_def_jid = j_rec.jid) ct2, 
             (SELECT COUNT(*) cnt
              FROM   subquery
              WHERE st_num IN (8,9,10,11,12) 
              AND   app_def_jid = j_rec.jid) ct3;
    END LOOP;
  END pro_cal_app;
END online_pkg;
/
SHOW ERRORS

Re: PL/SQL DML on Collections Thanks [message #130524 is a reply to message #130506] Sun, 31 July 2005 20:01 Go to previous messageGo to next message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry,

How can i drop this type and object in pl/sql

create type app_obj is object(ST_NUM number,A_JID number,CURR_JID number);
/

create type app_tab is table of app_obj;
/



SQL>drop type app_tab; successfully drops it.


sql>drop type app_obj; ----->>>???? its object how can i drop that.

Thanks
Sam
Re: PL/SQL DML on Collections Thanks [message #130548 is a reply to message #130524] Mon, 01 August 2005 00:09 Go to previous messageGo to previous message
mymot
Messages: 225
Registered: July 2005
Senior Member
Dmitry

Drop type app_obj; is worked.

Cheers
Sam
Previous Topic: NVL in Obejct alternate in PL/SQL
Next Topic: 10g trigger on selects ?? Trying to audit all selects of ssn
Goto Forum:
  


Current Time: Mon Aug 25 10:03:53 CDT 2025