oracle - 将包输出结果写入客户端的文件

时间:2015-04-01 12:27:41

标签: oracle plsql

所以我有这个:

CREATE OR REPLACE PACKAGE my_first_package
IS
 PROCEDURE employee_analysis
  (p_id      IN NUMBER := 100, /*default formal parameter with no arguments for invokation */
   p_percent IN NUMBER := 0.01); /* -||- */
END my_first_package;   
CREATE OR REPLACE PACKAGE BODY my_first_package
IS
  PROCEDURE employee_analysis
  (p_id      IN NUMBER := 100,
   p_percent IN NUMBER := 0.01)
  IS 
  CURSOR c_city IS
    (SELECT l.city
     FROM employees e
     INNER JOIN departments d
     ON (e.department_id = d.department_id)
     INNER JOIN locations l
     ON (l.location_id = d.location_id)
     WHERE e.employee_id = p_id);
 CURSOR c_manager IS
    (SELECT e1.last_name
     FROM employees e1
     INNER JOIN
          employees e2  
     ON (e1.employee_id = e2.manager_id)
     WHERE e2.employee_id = p_id);
 CURSOR c_department_name IS
    (SELECT department_name
     FROM employees e
     INNER JOIN
     departments d
     ON (e.department_id = d.department_id)
     WHERE e.employee_id = p_id);
   /*-----------------------------------------------------------------------------*/
  v_annual_sal          NUMBER(9,2);
  v_monthly_sal         NUMBER(9,2);
  v_last_name           VARCHAR2(10);
  v_deptno              NUMBER(3);
  v_length              NUMBER(2);
  v_tenure              NUMBER(5);
  v_job_id              VARCHAR2(20);
  v_hire_date           DATE;
  v_city                VARCHAR(25);
  v_commission_pct      NUMBER(2,2);
  v_phone_number        VARCHAR2(20);
  v_manager             VARCHAR2(20);
  v_comm_calc           NUMBER(10,2);
  v_email               VARCHAR2(10);
  v_department          VARCHAR2(20);
  v_count               NUMBER(4);
  v_old_salary          NUMBER(9,2);
  v_new_salary          NUMBER(9,2);
  v_lname               VARCHAR2(10);
  v_phone_number_format VARCHAR2(25);
  v_phone_number_length NUMBER(3);
  v_tax                 NUMBER(8,4);
  v_sum_sal_departments NUMBER;
 BEGIN
  DBMS_OUTPUT.PUT_LINE('Welcome to the summary of an employee based on his unique id');
  DBMS_OUTPUT.PUT_LINE('============================================================');
  /*-----------------------------------------------------------------------------*/
  SELECT salary, last_name, department_id, 
         TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date),0), job_id,
         hire_date, commission_pct, phone_number, email, LENGTH(phone_number)
  INTO   v_monthly_sal, v_last_name, v_deptno, 
         v_tenure, v_job_id, v_hire_date, v_commission_pct,
         v_phone_number, v_email, v_phone_number_length
  FROM   employees
  WHERE  employee_id = p_id;
  /*-----------------------------------------------------------------------------*/
  v_count := SQL%ROWCOUNT;
  DBMS_OUTPUT.PUT_LINE(v_count||' Employee found...');

  /*-----------------------------------------------------------------------------*/
  v_annual_sal := v_monthly_sal * 12;
  v_length := LENGTH(v_last_name);
  DBMS_OUTPUT.PUT_LINE('Employee:-> ' || v_last_name || ' ,and his name contains: ' || v_length ||' chars');

  DBMS_OUTPUT.PUT_LINE(q'[Belong's to department: ]' || v_deptno);

  /*-----------------------------------------------------------------------------*/
  IF (v_monthly_sal < v_annual_sal) 
  THEN
  DBMS_OUTPUT.PUT_LINE('Has a annual salary of:-> ' || v_annual_sal);
  ELSE
  DBMS_OUTPUT.PUT_LINE('Something wrong in the formula!');
  END IF;
  /*-------------------------------------------------------------------------------*/

  IF v_commission_pct IS NULL
  THEN
  DBMS_OUTPUT.PUT_LINE('No Commission added to the annual salary!');
  ELSE
  DBMS_OUTPUT.PUT_LINE('Commission percentage to the salary is:-> '|| v_commission_pct ||'%');
  v_comm_calc := (v_annual_sal * v_commission_pct) + v_annual_sal;
  DBMS_OUTPUT.PUT_LINE('And calculated with annual salary is:-> ' ||v_comm_calc);
  END IF;
 /*-------------------------------------------------------------------------------*/
  DBMS_OUTPUT.PUT_LINE('Working for:-> '|| v_tenure || ' months as '|| v_job_id);
  DBMS_OUTPUT.PUT_LINE('Started in:-> '|| v_hire_date);

  /*-------------------------------------------------------------------------------*/
  IF v_phone_number_length = 12
   THEN
   v_phone_number_format := '(' || SUBSTR(v_phone_number,1,3) ||  ')' ||
                            '-' || SUBSTR(v_phone_number,5,3) ||
                            '-' || SUBSTR(v_phone_number,9,4);
   DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);                        
   ELSIF v_phone_number_length = 18
   THEN 
        v_phone_number_format :=  '(' || SUBSTR(v_phone_number,1,3) ||  ')' ||
                                  '-' || SUBSTR(v_phone_number,5,2) ||
                                  '-' || SUBSTR(v_phone_number,8,4) || '-'
                                      || SUBSTR(v_phone_number,13,6);
       DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);
   ELSE 
       DBMS_OUTPUT.PUT_LINE('Phone number digits not in range, check the length of the phone numbers from the table');
  END IF;  
 /*-------------------------------------------------------------------------------*/
 DBMS_OUTPUT.PUT_LINE('Email:-> '||v_email);

 /*-------------------------------------------------------------------------------*/
  OPEN c_city;
  FETCH c_city
  INTO v_city;
  IF c_city%FOUND 
  THEN 
    DBMS_OUTPUT.PUT_LINE('Location:-> '||v_city);
  ELSE 
    DBMS_OUTPUT.PUT_LINE('Employee location unknown');
  END IF;
  CLOSE c_city;
 /*-------------------------------------------------------------------------------*/

  OPEN c_manager;
  FETCH c_manager
  INTO v_manager;
  IF c_manager%FOUND
  THEN 
    DBMS_OUTPUT.PUT_LINE('Is in the eyes of manager:-> '||v_manager);
  ELSE 
    DBMS_OUTPUT.PUT_LINE('Slave '||v_last_name||' is free!');
  END IF;
  CLOSE c_manager;
 /*-------------------------------------------------------------------------------*/

  OPEN c_department_name;
  FETCH c_department_name
  INTO v_department;
  IF c_department_name%FOUND
  THEN 
    DBMS_OUTPUT.PUT_LINE('Department Name:-> '||v_department);
  ELSE 
    DBMS_OUTPUT.PUT_LINE('Employee ' ||v_last_name||' belongs to no department!');
  END IF;
/*--------------------------------------------------------------------------------*/ 

  DBMS_OUTPUT.PUT_LINE('Checking the current employee with id:-> '|| p_id ||'..');
  IF (check_sal2(p_id) IS NULL)
  THEN 
    DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception, therefore employee does not exist!');
  ELSIF (check_sal2(p_id))
  THEN 
    DBMS_OUTPUT.PUT_LINE('Employees salary > average of department '||v_deptno||' where he belongs.');
  ELSE 
    DBMS_OUTPUT.PUT_LINE('Salary < average of department '||v_deptno||', where he belongs.');
  END IF;

/*--------------------------------------------------------------------------------*/
  SELECT salary
  INTO  v_old_salary
  FROM employees
  WHERE employee_id = p_id;
  DBMS_OUTPUT.PUT_LINE('Before the raise of ' || p_percent || 
                       ' %, the salary was:-> '|| v_old_salary);
/*--------------------------------------------------------------------------------*/ 
  IF (p_percent > 0.01)
  THEN
  DBMS_OUTPUT.PUT_LINE('Maximum increase allowance for the moment is only 0.01 %, thus no increase in salary is made');
  ELSIF (p_percent < 0.01)
  THEN
  DBMS_OUTPUT.PUT_LINE('Minimum percent allowance for the moment is only 0.01 %, thus no increase in salary is made');
  ELSE
  UPDATE employees
  SET    salary = salary * (1 + p_percent/100)
  WHERE  employee_id = p_id;
  SELECT last_name, salary
  INTO   v_lname, v_new_salary
  FROM   employees
  WHERE  employee_id = p_id;
  DBMS_OUTPUT.PUT_LINE('After the raise of ' || p_percent || 
                       ' %, the salary is:-> '|| v_new_salary);  
  END IF;
/*--------------------------------------------------------------------------------*/

  DBMS_OUTPUT.PUT_LINE('===========================================================');
  DBMS_OUTPUT.PUT_LINE('===========================================================');
  DBMS_OUTPUT.PUT_LINE('===========================================================');
  FOR i IN (SELECT SUM(salary) AS "SUMY", department_id
            FROM employees
            GROUP BY department_id)
  LOOP
   IF i.department_id IS NULL
   THEN
   DBMS_OUTPUT.PUT_LINE('Unknown department '|| i.department_id ||' earns:-> '|| i.sumy);
   ELSE
   DBMS_OUTPUT.PUT_LINE('Department '|| i.department_id ||' earns:-> '|| i.sumy);
   END IF;
  END LOOP;
  FOR j IN (SELECT SUM(sumy) AS "DEP_SUM"
            FROM (SELECT SUM(salary) AS "SUMY"
                  FROM employees
                  GROUP BY department_id))
  LOOP
  v_sum_sal_departments := j.dep_sum;
  DBMS_OUTPUT.PUT_LINE('Total income on all departments:-> '|| j.dep_sum);
  END LOOP;

  SELECT taxes_pkg.tax(salary)
  INTO v_tax
  FROM employees
  WHERE employee_id = p_id;
  DBMS_OUTPUT.PUT_LINE('Salary after 0.08 % tax withdrawal:-> '|| v_tax);

  FOR k IN (SELECT last_name
            FROM employees
            WHERE salary = (SELECT MAX(salary)
                            FROM employees))
  LOOP
   DBMS_OUTPUT.PUT_LINE('Employee with the highest salary is:-> '||k.last_name);
  END LOOP;
  FOR k IN (SELECT last_name, hire_date
            FROM employees
            WHERE hire_date = (SELECT MAX(hire_date)
                               FROM employees))
  LOOP
   DBMS_OUTPUT.PUT_LINE('Our newest employees are:-> ' || k.last_name);
  END LOOP;
  FOR m IN (SELECT last_name
           FROM employees
           WHERE hire_date = (SELECT MIN(hire_date)
                              FROM employees))
  LOOP
  DBMS_OUTPUT.PUT_LINE('Our oldest employees are:-> '||m.last_name);
  END LOOP;
/*--------------------------------------------------------------------------------*/

 EXCEPTION
  WHEN NO_DATA_FOUND
  THEN 
      DBMS_OUTPUT.PUT_LINE('Employee with id:-> ' || p_id || ' does not exist, check data from your tables!');
  WHEN OTHERS
  THEN 
      DBMS_OUTPUT.PUT_LINE('Unknown propagation');
 END employee_analysis;
END my_first_package;

我搜索了一些叫做utl_file包的东西,用于从oracle数据库导出数据等等,但我不太懂,也没找到我想要的东西。基本上我想要的是,如何在运行上面这个无用的包之后“导出”屏幕上的所有输出?有没有办法修改包添加功能和东西?有可能的?感谢...

0 个答案:

没有答案