Home » SQL & PL/SQL » SQL & PL/SQL » better query other than multiple for loop's?
better query other than multiple for loop's? [message #637918] Fri, 29 May 2015 01:55 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

c1
col1
---
1
2
3


c2
col1
---
2
3

c3
col1
---
1
2
3


for c1 loop
for c2(c1.param)

end loop;
end loop;

for c1 loop
for c3(c1.param)

end loop;
end loop;


I need to check c1 data into the c2,c3 cursor's , so that's why i have writeen like above,can you please give me sameple on better on this?

Because, package taking too much time to run

Thank you

[Updated on: Fri, 29 May 2015 01:56]

Report message to a moderator

Re: better query other than multiple for loop's? [message #637921 is a reply to message #637918] Fri, 29 May 2015 02:22 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Does it mean that you want to know which C1 values exist in C2 and C3? Try INTERSECT set operator.
Re: better query other than multiple for loop's? [message #637922 is a reply to message #637921] Fri, 29 May 2015 02:32 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Dear Friend,

Please use UNION ALL on c2 and C3 and then perform INTERSECT operation with C1.

Please share table creation and insertion script for requesting output on orafaq forum.please use following url :

http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries004.htm

Re: better query other than multiple for loop's? [message #637926 is a reply to message #637921] Fri, 29 May 2015 03:32 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Does it mean that you want to know which C1 values exist in C2 and C3? Try INTERSECT set operator.

Hi Littlefoot, i need to pass c1 data into the c2,c3 cursor , if i got millions of records into the c1 then how?

and to implement the code in the Begin & end block

and i need to display the out put at once not one by one like

empno ename
---- ------
1 a

deptno dname
------ ----
10 vv


empno ename
---- ------
2 b

expected as ,
empno ename
---- ------
1 a
2 b

deptno dname
------ ----
10 vv


please help

[Updated on: Fri, 29 May 2015 03:35]

Report message to a moderator

Re: better query other than multiple for loop's? [message #637927 is a reply to message #637926] Fri, 29 May 2015 03:48 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
No you need to combine c1 and c2 into a single select and combine c1 and c3 into a single select.
Past that, as always, you've posted so little information that we can't give any more advise on what to do.
Re: better query other than multiple for loop's? [message #637930 is a reply to message #637927] Fri, 29 May 2015 04:44 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi
create table dept_table(deptno number, Id number);

insert into dept_table values(20, 105);

insert into dept_table values(10, 21);

insert into dept_table values(20, 105);

insert into dept_table values(30, 48);

insert into dept_table values(30, 48);

insert into dept_table values(30, 48);

insert into dept_table values(10, 28);
 
insert into dept_table values(10, 25);
----------------------------------------------------------------------------


create table emp_table(deptno number, Id number);
 
insert into emp_table values(20, 105);
insert into emp_table values(20, 50);
----------------------------------------------------------------------------
create table section_table(deptno number, Id number);
 
insert into section_table values(10, 21);
 
insert into section_table values(10, 28);
 
insert into section_table values(10, 25);
 
insert into section_table values(30, 21);
 
insert into section_table values(30, 48);
 
insert into section_table values(30, 95);
Re: better query other than multiple for loop's? [message #637937 is a reply to message #637930] Fri, 29 May 2015 06:31 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Dear Friend,

I hope in your case you are expecting records from emp_table which belongs to department/Sections:


select  *
from    emp_table
where   deptno
in(
    select  deptno
    from    emp_table
    intersect
    select  deptno
    from    dept_table
  );



In second case you are expecting section details,if it belongs to department table :


select  *
from    section_table
where   deptno
in(
    select  deptno
    from    section_table
    intersect
    select  deptno
    from    dept_table
  )

Re: better query other than multiple for loop's? [message #637939 is a reply to message #637937] Fri, 29 May 2015 08:04 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Those intersects are completely pointless.
This:
select  *
from    emp_table
where   deptno
in(
    select  deptno
    from    emp_table
    intersect
    select  deptno
    from    dept_table
  );

Is equivalent to this:
select  *
from    emp_table
where   deptno
in(select  deptno
   from    dept_table
  );


Re: better query other than multiple for loop's? [message #637940 is a reply to message #637918] Fri, 29 May 2015 09:43 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
NEVER do in PL/SQL, that which can be done in plain SQL.
Previous Topic: Increasing Initrans for Partitioned table without re-creating it
Next Topic: Identify bottlenecks in queries
Goto Forum:
  


Current Time: Fri Apr 26 12:52:19 CDT 2024