正如标题所述,我正在尝试创建一个过程来显示有关员工的所有信息(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声明不正确,我对下一步该做什么感到困惑。 非常感谢大家!
答案 0 :(得分:4)
另一种方式(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;
分享并享受。
答案 1 :(得分:2)
这是一个带注释的工作示例。请注意,它使用异常而不是IF
来处理未找到的员工。
另外,请记住,使用DBMS_OUTPUT
时必须启用它。在执行过程之前,在SQLPlus命令行中键入SET SERVEROUTPUT ON SIZE 10000
。大小是要报告的最大字符数,因此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;
答案 2 :(得分:0)
查询如:
select ...
from employees
where employee_id = 123
union all
select ...
from employees
where not exists (select null from employees where employee_id = 123)
...如果找到单个员工,则返还该员工,如果不是,则返还所有员工。
答案 3 :(得分:0)
Employee_History_inf
,就像创建的员工表Employee_Columns
信息Pre_Update
员工表可以在新表new_info
中插入Employee_History_inf