-----------------------------------------------------------------
-- PL/SQL: Anonymous block with procedure (Example 4.4.3)       -
-- Chapter 4; Oracle Programming -- A Primer                    -
--            by R. Sunderraman                                 -
-----------------------------------------------------------------

DECLARE
  cnum customers.cno%type;
  ccname customers.cname%type;
  cphone customers.phone%type;
  ccity zipcodes.city%type;
  status boolean;

  procedure get_cust_details(cust_no IN customers.cno%type,
                             cust_name OUT customers.cname%type,
                             cust_phone OUT customers.phone%type,
                             cust_city  OUT zipcodes.city%type,
                             status OUT boolean) is
  begin
    select cname,phone,city
    into   cust_name,cust_phone,cust_city
    from   customers,zipcodes
    where  customers.zip = zipcodes.zip and
           customers.cno = cust_no;
    status := true;

    exception
      when no_data_found then
        status := false;
  end;

BEGIN
  cnum := 1111;
  get_cust_details(cnum,ccname,cphone,ccity,status);
  if (status) then 
    DBMS_OUTPUT.PUT_LINE(cnum   || '  ' || 
                         ccname || '  ' ||  
                         cphone || '  ' ||  
                         ccity);
  else
    DBMS_OUTPUT.PUT_LINE('Customer ' || 
                          cnum       || 
                          ' not found');
  end if;
  cnum := 5555;
  get_cust_details(cnum,ccname,cphone,ccity,status);
  if (status) then 
    DBMS_OUTPUT.PUT_LINE(cnum   || '  ' || 
                         ccname || '  ' ||  
                         cphone || '  ' ||  
                         ccity);
  else
    DBMS_OUTPUT.PUT_LINE('Customer ' || 
                         cnum        || 
                         ' not found');
  end if;
END;
/
