0

正如标题所述,我正在尝试创建一个程序来显示有关员工的所有信息(Employee_ID、NAME、EMAIL_ADDRESS、HIRE_DATE 和 UPDATE_DATE)。问题是如果employee_id 不在表中,则输出应显示有关员工的所有信息。我要拍摄的输出是:

EXEC get_employee_info_by_employee_id(4565)

Employee_ID:       4565
NAME:              Bob Doe
EMAIL_ADDRESS:     bobdoe@gmail.com
HIRE_DATE:         30-JUN-10
UPDATE DATE:       13-JULY-12

这是我当前的代码:

create or replace PROCEDURE get_employee_info_by_employee_id 
(   
p_employee_id   NUMBER DEFAULT -1
)
AS
   v_count   NUMBER;
BEGIN
      SELECT  COUNT(*)
      INTO    v_count
      FROM    employee
      WHERE   employee_id = p_employee_id;

  IF p_employee_id IS NULL THEN
    DBMS_OUTPUT.PUT_LINE('Employee ID: ' || employee_id);
    DBMS_OUTPUT.PUT_LINE('NAME: ' || name);
    DBMS_OUTPUT.PUT_LINE('EMAIL_ADDRESS: ' || email_address);
    DBMS_OUTPUT.PUT_LINE('HIRE_DATE: ' || hire_date);
    DBMS_OUTPUT.PUT_LINE('UPDATE_DATE: ' || update_date);

  ELSE
      SELECT  COUNT(*)
      INTO    v_count
      FROM    employee
      WHERE   employee_id = p_employee_id;
  END IF;
END;

我知道这远非正确。作为一个初学者,我正在寻找如何编辑和使用我目前拥有的代码,并学习如何解决这个问题。我知道我的 ELSE 语句是不正确的,而且我对下一步该做什么感到困惑。非常感谢大家!

4

4 回答 4

3

另一种方式(tm):

create or replace PROCEDURE get_employee_info_by_employee_id 
    (p_employee_id   NUMBER DEFAULT NULL)
AS
BEGIN
   -- This cursor will return
   --   - a single row if p_employee_id is specified and exists in the table
   --   - all rows in the table if p_employee_id is NULL (default value, or
   --     passed in as NULL)
   --   - all rows in the table if p_employee_id is specified but does not
   --     exist in the table

   FOR aRow IN (SELECT EMPLOYEE_ID, Name, Email_Address, Hire_Date, Update_Date
                FROM Employee
                WHERE Employee_ID = p_employee_id OR
                      p_employee_id IS NULL OR
                      0 = (SELECT COUNT(*)
                             FROM EMPLOYEE
                             WHERE EMPLOYEE_ID = p_employee_id)
   LOOP
     DBMS_OUTPUT.PUT_LINE('Employee ID: ' || aRow.EMPLOYEE_ID);
     DBMS_OUTPUT.PUT_LINE('NAME: ' || aRow.NAME);
     DBMS_OUTPUT.PUT_LINE('EMAIL_ADDRESS: ' || aRow.EMAIL_ADDRESS);
     DBMS_OUTPUT.PUT_LINE('HIRE_DATE: ' || aRow.HIRE_DATE);
     DBMS_OUTPUT.PUT_LINE('UPDATE_DATE: ' || aRow.UPDATE_DATE);
   END LOOP;
END get_employee_info_by_employee_id;

分享和享受。

于 2013-05-23T16:43:39.627 回答
2

这是一个带有注释的工作示例。请注意,它使用异常而不是 anIF来处理未找到的员工。

另外,请记住,当您使用它时,DBMS_OUTPUT您必须启用它。SET SERVEROUTPUT ON SIZE 10000在执行过程之前在 SQLPlus 命令行中键入。大小是要报告的最大字符数,所以 10K 绰绰有余。

create or replace PROCEDURE get_employee_info_by_employee_id 
(   
p_employee_id   NUMBER DEFAULT -1
)
AS
   -- You need to query the values you're showing into variables. The
   -- variables can have the same name as the column names. Oracle won't
   -- be confused by this, but I usually am - that's why I have the "v_"
   -- prefix for the variable names here. Finally, when declaring the
   -- variable's type, you can reference table.column%TYPE to use the
   -- type of an existing column.
   v_name Employee.Name%TYPE;
   v_email_address Employee.Email_Address%TYPE;
   v_hire_date Employee.Hire_Date%TYPE;
   v_update_date Employee.Update_Date%TYPE;
BEGIN
   -- Just SELECT away, returning column values into the variables. If
   -- the employee ID isn't found, Oracle will throw and you can pick
   -- up the pieces in the EXCEPTION block below.
   SELECT Name, Email_Address, Hire_Date, Update_Date
     INTO v_name, v_email_address, v_hire_date, v_update_date
     FROM Employee
     WHERE Employee_ID = p_employee_id;
   -- Fallthrough to here means the query above found one (and only one)
   -- row, and therefore it put values into the variables. Print out the
   -- variables.
   --
   -- Also note there wasn't a v_employee_id variable defined, because
   -- you can use your parameter value (p_employee_id) for that.
   DBMS_OUTPUT.PUT_LINE('Employee ID: ' || p_employee_id);
   DBMS_OUTPUT.PUT_LINE('NAME: ' || v_name);
   DBMS_OUTPUT.PUT_LINE('EMAIL_ADDRESS: ' || v_email_address);
   DBMS_OUTPUT.PUT_LINE('HIRE_DATE: ' || v_hire_date);
   DBMS_OUTPUT.PUT_LINE('UPDATE_DATE: ' || v_update_date);
EXCEPTION
   -- If the query didn't find a row you'll end up here. In this case
   -- there's no need for any type of fancy exception handling; just
   -- reporting that the employee wasn't found is enough.
   WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('Employee number ' || p_employee_id || ' not found.');
END;
于 2013-05-23T16:23:17.577 回答
0

一个查询,例如:

select ...
from   employees
where  employee_id = 123
union all
select ...
from   employees
where  not exists (select null from employees where employee_id = 123)

...如果找到,将返回单个员工,如果没有,则返回所有员工。

于 2013-05-23T16:11:23.200 回答
0
  1. 您可以Employee_History_inf像创建的员工表一样创建新表
  2. 使用参数创建过程作为Employee_Columns员工表上的信息
  3. 创建触发器Pre_Update员工表可以插入new_info到您的新表中Employee_History_inf
于 2018-12-27T09:52:10.393 回答