Home » SQL & PL/SQL » SQL & PL/SQL » don't want to print line if records are not found
don't want to print line if records are not found [message #604623] Thu, 02 January 2014 02:54 Go to next message
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 #604624 is a reply to message #604623] Thu, 02 January 2014 02:57 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So add an exists clause to dept_cur so it only returns depts that have emps.
If there's data you don't want the best solution is almost always to not select it in the first place.
Re: don't want to print line if records are not found [message #604626 is a reply to message #604624] Thu, 02 January 2014 03:00 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi cookiemonster,

Thank you for early reply, It works for me when i used where deptno in(10,20,30) and I write exists clause it also , prints department no 40 .could you please suggest me how to write exist clause query..

where exists (select deptno from emp)



Thank You

[Updated on: Thu, 02 January 2014 03:08]

Report message to a moderator

Re: don't want to print line if records are not found [message #604629 is a reply to message #604626] Thu, 02 January 2014 03:15 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
mist598 wrote on Thu, 02 January 2014 18:00
Hi cookiemonster,

Thank you for early reply, It works for me when i used where deptno in(10,20,30) and I write exists clause it also , prints department no 40 .could you please suggest me how to write exist clause query..

where exists (select deptno from emp)



Thank You


Thanks to cookiemonster and Lalit Kumar B
It's working fine when i use exists where cookiemonster suggest me.
      SELECT deptno 
      FROM   dept a
      where exists (select deptno from emp b where a.deptno=b.deptno(+))


Thank You..Smile)
Re: don't want to print line if records are not found [message #604630 is a reply to message #604626] Thu, 02 January 2014 03:16 Go to previous messageGo to next message
gauravgautam135
Messages: 33
Registered: December 2013
Member
Use your dept_cur as shown below:

CURSOR dept_cur IS 
SELECT deptno d
 WHERE EXISTS (SELECT 1 FROM emp e where e.deptno = d.deptno)
  FROM dept 
 ORDER BY deptno; 
Re: don't want to print line if records are not found [message #604631 is a reply to message #604629] Thu, 02 January 2014 03:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That join doesn't need to be an outer join.
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #604637 is a reply to message #604632] Thu, 02 January 2014 03:32 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thanks to all..Smile))

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


Quote:
Do you really need PL/SQL for this task? Any specific reason?

Yes for only PL/SQL and also In Reports.


if i use below code it is working fine.
WHERE EXISTS (SELECT 1 FROM emp e where e.deptno = d.deptno)
                  (AND)
WHERE deptno IN (SELECT DEPTNO FROM EMP)



Once again Thank You
Re: don't want to print line if records are not found [message #604640 is a reply to message #604637] Thu, 02 January 2014 03:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that my sentence is true because there is a foreign key from emp table referencing dept one.

Re: don't want to print line if records are not found [message #604641 is a reply to message #604640] Thu, 02 January 2014 03:58 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi Michel Cadot,

Quote:
As you don't use anything from dept table then the first cursor is useless

And i follow your code only , Could you please explain me please what is refer to the above on Qoute.

Thank You
Re: don't want to print line if records are not found [message #604643 is a reply to message #604635] Thu, 02 January 2014 04:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Michel Cadot wrote on Thu, 02 January 2014 09:28

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 ; 


That needs an order by surely?
Re: don't want to print line if records are not found [message #604644 is a reply to message #604641] Thu, 02 January 2014 04:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Sorry I don't understand your question.

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 Go to previous messageGo to next message
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 #604648 is a reply to message #604643] Thu, 02 January 2014 04:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
cookiemonster wrote on Thu, 02 January 2014 11:06
...
That needs an order by surely?


SQL strictly speaking, yes. Smile
Here I use a side effect that analytic function is calculated in last part of statement execution and so the "partition by" and "order by" applies to the whole returned result set. So rows are grouped by "partition by" parameter and in each group by "order by" parameter. In short, rows are sorted in each group but groups are not sorted (despite the fact they seem to be in my output). Wink
But yes, you are right, I was too lazy and should add an ORDER BY clause to my query.

Re: don't want to print line if records are not found [message #604649 is a reply to message #604648] Thu, 02 January 2014 04:18 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

Quote:
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.

Thank You..-)


Re: don't want to print line if records are not found [message #604650 is a reply to message #604648] Thu, 02 January 2014 04:19 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Isn't that like relying on the side effect of group by sorting in older versions?
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 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
mist598 wrote on Thu, 02 January 2014 10:18
Quote:
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 #604653 is a reply to message #604652] Thu, 02 January 2014 04:23 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thank You cookiemonster ..-)
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 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
cookiemonster wrote on Thu, 02 January 2014 11:19
Isn'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!

Previous Topic: How to create outer Join in this Query
Next Topic: Please help me to rewrite this
Goto Forum:
  


Current Time: Fri Apr 19 20:10:38 CDT 2024