don't want to print line if records are not found [message #604623] |
Thu, 02 January 2014 02:54 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Hi all,
At below code i don't want to print if records are not in deptno=40
DECLARE
CURSOR dept_cur
IS
SELECT deptno
FROM dept
ORDER BY deptno;
-- Employee cursor all employees for a dept number
CURSOR emp_cur ( v_dept_no dept.deptno%TYPE)
IS
SELECT ename
FROM emp
WHERE deptno = v_dept_no;
BEGIN
FOR dept_rec IN dept_cur LOOP
dbms_output.Put_line('Employees in Department ' ||' ' ||To_char(dept_rec.deptno));
FOR emp_rec IN emp_cur(dept_rec.deptno) LOOP
dbms_output.Put_line('...Employee is '||' '||emp_rec.ename);
END LOOP;
END LOOP;
END;
Output;
Employees in Department 30
...Employee is BLAKE
...Employee is ALLEN
...Employee is WARD
...Employee is MARTIN
...Employee is TURNER
...Employee is JAMES
Employees in Department 40 -->Here(don't want to print this line)
Thank You
|
|
|
|
|
|
|
|
Re: don't want to print line if records are not found [message #604632 is a reply to message #604626] |
Thu, 02 January 2014 03:19 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
SQL> DROP TABLE dept;
Table dropped
SQL> DROP TABLE emp;
Table dropped
SQL> CREATE TABLE dept(deptno NUMBER);
Table created
SQL> CREATE TABLE emp(empno NUMBER, deptno NUMBER);
Table created
SQL> INSERT INTO dept VALUES(10);
1 row inserted
SQL> INSERT INTO dept VALUES(20);
1 row inserted
SQL> INSERT INTO dept VALUES(30);
1 row inserted
SQL> INSERT INTO emp VALUES(1000,10);
1 row inserted
SQL> INSERT INTO emp VALUES(1001,10);
1 row inserted
SQL> INSERT INTO emp VALUES(1002,10);
1 row inserted
SQL> INSERT INTO emp VALUES(2000,20);
1 row inserted
SQL> INSERT INTO emp VALUES(2001,20);
1 row inserted
SQL> INSERT INTO emp VALUES(2002,20);
1 row inserted
SQL> COMMIT;
Commit complete
SQL> set serveroutput on;
SQL>
SQL> DECLARE
2 CURSOR dept_cur
3 IS
4 SELECT DEPTNO
5 FROM DEPT
6 WHERE deptno IN (SELECT DEPTNO FROM EMP)
7 ORDER BY deptno;
8 -- Employee cursor all employees for a dept number
9 CURSOR emp_cur ( v_dept_no dept.deptno%TYPE)
10 IS
11 SELECT empno
12 FROM emp
13 WHERE deptno = v_dept_no;
14 BEGIN
15 FOR dept_rec IN dept_cur LOOP
16 dbms_output.Put_line('Employees in Department ' ||' ' ||To_char(dept_rec.deptno));
17
18 FOR emp_rec IN emp_cur(dept_rec.deptno) LOOP
19 dbms_output.Put_line('...Employee is '||' '||emp_rec.empno);
20 END LOOP;
21 END LOOP;
22 END;
23 /
Employees in Department 10
...Employee is 1000
...Employee is 1001
...Employee is 1002
Employees in Department 20
...Employee is 2000
...Employee is 2001
...Employee is 2002
PL/SQL procedure successfully completed
[Updated on: Thu, 02 January 2014 03:21] Report message to a moderator
|
|
|
Re: don't want to print line if records are not found [message #604635 is a reply to message #604623] |
Thu, 02 January 2014 03:28 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
As you don't use anything from dept table then the first cursor is useless:
SQL> DECLARE
2 CURSOR emp_cur
3 IS
4 SELECT deptno, ename,
5 row_number() over (partition by deptno order by ename) rn
6 FROM emp ;
7 BEGIN
8 FOR emp_rec IN emp_cur LOOP
9 if emp_rec.rn = 1 then
10 dbms_output.Put_line('Employees in Department ' ||' ' ||To_char(emp_rec.deptno));
11 end if;
12 dbms_output.Put_line('...Employee is '||' '||emp_rec.ename);
13 END LOOP;
14 END;
15 /
Employees in Department 10
...Employee is CLARK
...Employee is KING
...Employee is MILLER
Employees in Department 20
...Employee is ADAMS
...Employee is FORD
...Employee is JONES
...Employee is SCOTT
...Employee is SMITH
Employees in Department 30
...Employee is ALLEN
...Employee is BLAKE
...Employee is JAMES
...Employee is MARTIN
...Employee is TURNER
...Employee is WARD
PL/SQL procedure successfully completed.
[Updated on: Thu, 02 January 2014 03:28] Report message to a moderator
|
|
|
Re: don't want to print line if records are not found [message #604636 is a reply to message #604632] |
Thu, 02 January 2014 03:32 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
And that entire PL/SQL code can be replaced with plain SQL :
SQL> SELECT 'For Department no : ' || D.DEPTNO || CHR(10) ||
2 'Employee numbers are ' || CHR(10) || LISTAGG(EMPNO, ',' || CHR(10)) WITHIN GROUP(ORDER BY EMPNO) AS EMPLOYEE_NO
3 FROM DEPT D, EMP E
4 WHERE D.DEPTNO = E.DEPTNO
5 GROUP BY D.DEPTNO;
EMPLOYEE_NO
--------------------------------------------------------------------------------
For Department no : 10
Employee numbers are
1000,
1001,
1002
For Department no : 20
Employee numbers are
2000,
2001,
2002
|
|
|
|
|
|
|
|
Re: don't want to print line if records are not found [message #604645 is a reply to message #604637] |
Thu, 02 January 2014 04:07 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
mist598 wrote on Thu, 02 January 2014 09:32
Quote:That join doesn't need to be an outer join.
Yes.Because suppose if use a.deptno=b.deptno(+) it is working fine , but if i this join condition use a.deptno(+)=b.deptno
i got error:
ORA-01705: an outer join cannot be specified on a correlation column
I said you didn't need an outer join, not that the outer join needed to be the other way around, just remove the (+).
|
|
|
|
|
|
Re: don't want to print line if records are not found [message #604652 is a reply to message #604649] |
Thu, 02 January 2014 04:21 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
mist598 wrote on Thu, 02 January 2014 10:18Quote:I said you didn't need an outer join, not that the outer join needed to be the other way around,just remove the (+).
Ok.
At post#604639 Michel Cadot Replied me as.
Quote:As you don't use anything from dept table then the first cursor is useless
I said yes.
Could you please what is the exact meaning refers to.
There's nothing you get from the first cursor that can't be got from the 2nd, as Michel's example demonstrates.
|
|
|
|
Re: don't want to print line if records are not found [message #604656 is a reply to message #604650] |
Thu, 02 January 2014 04:36 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
cookiemonster wrote on Thu, 02 January 2014 11:19Isn't that like relying on the side effect of group by sorting in older versions?
No, as I said, groups may not be sorted between them (30 may appear before 20) but in each group rows are sorted accordingly to "order by" clause of "row_number" function.
This was so true that in a earlier version there was a bug so that if you sorted in row_number using, for instance, ename and add an order by clause to the query with "ename desc" this later one was ignored and you had the rows ordering by ename ascending!
|
|
|