cursor for loop [message #601046] |
Fri, 15 November 2013 01:00 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Hi all,
I want to display deptno 10 and it's employee details and also 20,30,40 , But here it displays all the details
HELLO 10 ACCOUNTING
7839 KING
7698 BLAKE
7782 CLARK
7566 JONES
7788 SCOTT
7902 FORD
7369 SMITH
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7876 ADAMS
7900 JAMES
7934 MILLER
HELLO 20 RESEARCH
7839 KING
7698 BLAKE
7782 CLARK
7566 JONES
7788 SCOTT
7902 FORD
7369 SMITH
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7876 ADAMS
7900 JAMES
7934 MILLER
HELLO 30 SALES
7839 KING
7698 BLAKE
7782 CLARK
7566 JONES
7788 SCOTT
7902 FORD
7369 SMITH
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7876 ADAMS
7900 JAMES
7934 MILLER
HELLO 40 OPERATIONS
7839 KING
7698 BLAKE
7782 CLARK
7566 JONES
7788 SCOTT
7902 FORD
7369 SMITH
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7876 ADAMS
7900 JAMES
7934 MILLER
56
DECLARE
emp_dept_id emp.deptno%TYPE;
emp_emp_id emp.empno%TYPE;
emp_last_name emp.ename%TYPE;
v_count number DEFAULT 0;
BEGIN
FOR i IN (SELECT DISTINCT deptno, dname
FROM dept)
LOOP
--v_COUNT := v_COUNT + 1;
DBMS_OUTPUT.PUT_LINE('HELLO'||' '||i.deptno||' '||i.dname);
FOR j IN (SELECT empno, ename
INTO emp_emp_id,emp_last_name
FROM emp)
LOOP
DBMS_OUTPUT.PUT_LINE(j.empno||' '||j.ename);
v_COUNT := v_COUNT + 1;
END LOOP;
END LOOP;
DBMS_OUTPUT.PUT_LINE(v_COUNT);
END;
Can any one suggest
Thank you
|
|
|
|
|
Re: cursor for loop [message #601051 is a reply to message #601046] |
Fri, 15 November 2013 01:17 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
You ask to display 10, 20, 30 and 40, you display 10, 2o, 30 and 40. what is the problem, it looks like you are doing what you want
|
|
|
Re: cursor for loop [message #601052 is a reply to message #601050] |
Fri, 15 November 2013 01:19 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
loop 1.outside for loop 10 from dept
inside for loop 14 records from emp table --restrict for only getting 10
loop 2.outside for loop 20 from dept
inside for loop 14 records from emp table --restrict for only getting 20
and so on ...
[Updated on: Fri, 15 November 2013 01:21] Report message to a moderator
|
|
|
|
Re: cursor for loop [message #601058 is a reply to message #601046] |
Fri, 15 November 2013 02:09 |
|
dariyoosh
Messages: 538 Registered: March 2009 Location: France
|
Senior Member |
|
|
mist598 wrote on Fri, 15 November 2013 08:00I want to display deptno 10 and it's employee details and also 20,30,40 , But here it displays all the details
Maybe I didn't understand the problem, but isn't it a simple SELECT? I don't see why a PL/SQL solution has been implemented.
[EDIT:] Sorry, now looking more closely to the your sample output I see the problem, sorry for mixing up!
[Updated on: Fri, 15 November 2013 02:11] Report message to a moderator
|
|
|
|
Re: cursor for loop [message #601106 is a reply to message #601058] |
Fri, 15 November 2013 14:43 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
dariyoosh wrote on Fri, 15 November 2013 03:09Sorry, now looking more closely to the your sample output I see the problem, sorry for mixing up!
Just for fun. Using grouping sets:
select case grouping(empno)
when 0 then empno || ' ' || ename
else 'HELLO' || ' ' || d.deptno || ' '|| d.dname
end emp_list
from emp e,
dept d
where d.deptno = e.deptno(+)
having grouping(empno) = 1 or empno is not null
group by grouping sets((d.deptno,dname),(d.deptno,empno,ename,dname))
order by d.deptno,
grouping(empno) desc
/
EMP_LIST
-----------------------
HELLO 10 ACCOUNTING
7934 MILLER
7839 KING
7782 CLARK
HELLO 20 RESEARCH
7876 ADAMS
7788 SCOTT
7566 JONES
7369 SMITH
7902 FORD
HELLO 30 SALES
EMP_LIST
-----------------------
7900 JAMES
7499 ALLEN
7521 WARD
7654 MARTIN
7698 BLAKE
7844 TURNER
HELLO 40 OPERATIONS
18 rows selected.
SCOTT@orcl >
SY.
|
|
|
|