在oracle中的过程中使用多个表

时间:2016-05-12 01:53:21

标签: oracle

我是PL / SQL的新手。有人可以帮我解决以下情况。 如何获取员工详细信息,如emp_id,full_name,department_id,total_salary 如果我们将Postal代码作为输入参数提供给过程。我知道如何从一个表中为员工详细信息编写过程,但在这里我们必须使用三个表。 任何人都可以帮忙。 我也提供了我用过的代码

 Create or Replace Procedure P1(postal_cd varchar2)
 as
     v_empid    number;
     v_name     varchar2(20);
     v_dept_Id  number(10);
     v_dept_nm  varchar2(10);
     v_hiredate date;
     v_sal      number;
 begin
     select e.emp_id
          , e.full_name||' '||e.last_name
          , e.department_id
          , d.department_name
          , e.hiredata
          , sum(e.sal) salary
       into v_empid
          , v_name
          , v_dept_id
          , v_dept_nm
          , v_hiredate
          , v_sal
       from employees   e
 inner join departments d
         on e.department_id = d.department_id
 inner join locations   l
         on d.location_id   = l.location.id
      where l.postal_code = postal_cd
          ;
 end;

如果上述查询给出了我所需的结果,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

由于您希望显示属于特定邮政编码的员工详细信息,因此以下代码应该有所帮助。

CREATE OR REPLACE PROCEDURE p1(postal_cd VARCHAR2)
AS
     v_empid    NUMBER;
     v_name     VARCHAR2(20);
     v_dept_Id  NUMBER(10);
     v_dept_nm  VARCHAR2(10);
     v_hiredate DATE;
     v_sal      NUMBER;

     CURSOR cur_employee 
     IS
     SELECT e.emp_id, 
            e.full_name||' '||e.last_name AS emp_full_name, 
            e.department_id, 
            d.department_name,
            e.hiredata, 
            e.sal 
     INTO   v_empid, 
            v_name, 
            v_dept_id,
            v_dept_nm, 
            v_hiredate, 
            v_sal
     FROM   employees   e
     INNER  JOIN departments d ON e.department_id = d.department_id
     INNER  JOIN locations l ON d.location_id   = l.location_id
     WHERE  l.postal_code = postal_cd;


BEGIN

     FOR c_employee IN cur_employee
     LOOP
         DBMS_OUTPUT.PUT_LINE(c_employee.emp_full_name || ' ' ||c_employee.department_id||' '||c_employee.department_name||' '||c_employee.hiredata ||' ' || c_employee.sal);
     END LOOP;

END;