-----------------------------------------------------------------
-- PL/SQL: Cursor Variable example (Section 4.6.4)              -
-- Chapter 4; Oracle Programming -- A Primer                    -
--            by R. Sunderraman                                 -
-----------------------------------------------------------------

create or replace procedure display_table(tname IN varchar2) AS
  TYPE cur_type IS REF CURSOR;

  invalid_table_error EXCEPTION;
  c1 cur_type;
  emp_rec  employees%rowtype;
  cust_rec customers%rowtype;
  zip_rec  zipcodes%rowtype;
  ord_rec  orders%rowtype;
  od_rec   odetails%rowtype;
  part_rec parts%rowtype;

BEGIN
  if upper(tname) = 'CUSTOMERS' then
    open c1 for select * from customers;
  elsif upper(tname) = 'EMPLOYEES' then 
    open c1 for select * from employees;
  elsif upper(tname) = 'ORDERS' then 
    open c1 for select * from orders;
  elsif upper(tname) = 'ODETAILS' then 
    open c1 for select * from odetails;
  elsif upper(tname) = 'PARTS' then 
    open c1 for select * from parts;
  elsif upper(tname) = 'ZIPCODES' then 
    open c1 for select * from zipcodes;
  else
    raise invalid_table_error;
  end if;

  loop
    if upper(tname) = 'CUSTOMERS' then
      fetch c1 into cust_rec;
      exit when c1%notfound;
      dbms_output.put_line(cust_rec.cno || ' ' || 
                           cust_rec.cname || ' ' ||
                           cust_rec.street || ' ' ||
                           cust_rec.zip || ' ' ||
                           cust_rec.phone); 
    elsif upper(tname) = 'EMPLOYEES' then
      fetch c1 into emp_rec;
      exit when c1%notfound;
      dbms_output.put_line(emp_rec.eno || ' ' || 
                           emp_rec.ename || ' ' ||
                           emp_rec.zip || ' ' ||
                           emp_rec.hdate); 
    elsif upper(tname) = 'PARTS' then
      fetch c1 into part_rec;
      exit when c1%notfound;
      dbms_output.put_line(part_rec.pno || ' ' || 
                           part_rec.pname || ' ' ||
                           part_rec.qoh || ' ' ||
                           part_rec.price || ' ' ||
                           part_rec.olevel); 
    elsif upper(tname) = 'ZIPCODES' then
      fetch c1 into zip_rec;
      exit when c1%notfound;
      dbms_output.put_line(zip_rec.zip || ' ' || 
                           zip_rec.city); 
    elsif upper(tname) = 'ORDERS' then
      fetch c1 into ord_rec;
      exit when c1%notfound;
      dbms_output.put_line(ord_rec.ono || ' ' || 
                           ord_rec.cno || ' ' ||
                           ord_rec.eno || ' ' ||
                           ord_rec.received || ' ' ||
                           ord_rec.shipped); 
    elsif upper(tname) = 'ODETAILS' then
      fetch c1 into od_rec;
      exit when c1%notfound;
      dbms_output.put_line(od_rec.ono || ' ' || 
                           od_rec.pno || ' ' ||
                           od_rec.qty); 
    end if;
  end loop;
  close c1;
  
  EXCEPTION 
    WHEN invalid_table_error THEN
      dbms_output.put_line('NOT A VALID TABLE NAME');
      
END;
/
