Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:43:51+00:00 2026-05-30T10:43:51+00:00

I’m trying to create a pl/sql block that uses references. Basicly, this is what

  • 0

I’m trying to create a pl/sql block that uses references. Basicly, this is what I’ve got so far. They both work as individual cursors. I’m finding it a little difficult with the reference. What I need is to list out department name, then go through to the emp_cursor and list all of the employees that are in that are in department_id = outer cursor department _id.

set serveroutput on 
declare
  cursor dept_cursor is 
    select department_id, department_name
    from departments 
    where department_id < 100;
    dep_id departments.department_id%type;
    dep_name departments.department_name%type;

    cursor emp_cursor is 
      select last_name, job_id, hire_date, salary
      from employees 
      where employee_id < 120 and department_id =/*Need reference from deptartmens.departments_id*/department_id;
      emp_ln employees.last_name%type;
      emp_jid employees.job_id%type;
      emp_hd employees.hire_date%type;
      emp_sal employees.salary%type;

begin
    for rec_dept in dept_cursor loop
      dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
      dbms_output.put_line('---------------------------------------------');
      for rec_emp in emp_cursor loop
        dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
      end loop;
      dbms_output.put_line('---------------------------------------------');
    end loop;

end ;

For example some output would look like this.

Department Number : 10  Department Name : Administration
----------------------------------------------------------------------------------------
Department Number : 20  Department Name : Marketing
----------------------------------------------------------------------------------------
Department Number : 30  Department Name : Purchasing
Raphaely    PU_MAN   07-DEC-94   11000
Khoo    PU_CLERK   18-MAY-95   3100
Baida    PU_CLERK   24-DEC-97   2900
Tobias    PU_CLERK   24-JUL-97   2800
Himuro    PU_CLERK   15-NOV-98   2600
Colmenares    PU_CLERK   10-AUG-99   2500
----------------------------------------------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:43:53+00:00Added an answer on May 30, 2026 at 10:43 am

    You can declare EMP_CURSOR as a parameterized cursor. Something like

    declare
      cursor dept_cursor is
        select department_id, department_name
        from departments
        where department_id < 100;
        dep_id departments.department_id%type;
        dep_name departments.department_name%type;
      cursor emp_cursor( p_department_id IN NUMBER ) is
        select last_name, job_id, hire_date, salary
        from employees
        where employee_id < 120
          and department_id = p_department_id;
      emp_ln employees.last_name%type;
      emp_jid employees.job_id%type;
      emp_hd employees.hire_date%type;
      emp_sal employees.salary%type;
    begin
      for rec_dept in dept_cursor loop
        dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
        dbms_output.put_line('---------------------------------------------');
        for rec_emp in emp_cursor( rec_dept.department_id ) loop
          dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
        end loop;
        dbms_output.put_line('---------------------------------------------');
      end loop;
    end ;
    

    which generates the following output in the HR schema

    Department Number: 10   Department Name: Administration
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 20   Department Name: Marketing
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 30   Department Name: Purchasing
    ---------------------------------------------
    Raphaely   PU_MAN   07-DEC-02   11000
    Khoo   PU_CLERK   18-MAY-03   3100
    Baida   PU_CLERK   24-DEC-05   2900
    Tobias   PU_CLERK   24-JUL-05   2800
    Himuro   PU_CLERK   15-NOV-06   2600
    Colmenares   PU_CLERK   10-AUG-07   2500
    ---------------------------------------------
    Department Number: 40   Department Name: Human Resources
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 50   Department Name: Shipping
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 60   Department Name: IT
    ---------------------------------------------
    Hunold   IT_PROG   03-JAN-06   9000
    Ernst   IT_PROG   21-MAY-07   6000
    Austin   IT_PROG   25-JUN-05   4800
    Pataballa   IT_PROG   05-FEB-06   4800
    Lorentz   IT_PROG   07-FEB-07   4200
    ---------------------------------------------
    Department Number: 70   Department Name: Public Relations
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 80   Department Name: Sales
    ---------------------------------------------
    ---------------------------------------------
    Department Number: 90   Department Name: Executive
    ---------------------------------------------
    King   AD_PRES   17-JUN-03   24000
    Kochhar   AD_VP   21-SEP-05   17000
    De Haan   AD_VP   13-JAN-01   17000
    ---------------------------------------------
    

    Now, from a performance standpoint, you would be much better served to join the two tables rather than writing your own nested loop in PL/SQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.