我正在编写一个简单的CASE,以便在员工获得更多部门的AVG工资时在屏幕上打印。但是我不明白为什么当我想检查2名员工的薪水时,我在屏幕上只看到一个结果。我希望你理解我的问题。感谢。
SET SERVEROUTPUT ON
DECLARE
v_emp121_sal employees.salary%TYPE;
v_emp121_lname employees.last_name%TYPE;
v_emp139_sal employees.salary%TYPE;
v_emp139_lname employees.last_name%TYPE;
v_avgsal employees.salary%TYPE;
BEGIN
SELECT salary, last_name
INTO v_emp121_sal, v_emp121_lname
FROM employees
WHERE employee_id = 121;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE 121 SALARY IS: ' || v_emp121_sal);
SELECT salary, last_name
INTO v_emp139_sal, v_emp139_lname
FROM employees
WHERE employee_id = 139;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE 139 SALARY IS: ' || v_emp139_sal);
SELECT AVG(salary)
INTO v_avgsal
FROM employees
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE('DEPARTMENT 50 AVG SALARY IS: ' ||
TRUNC(v_avgsal, 0));
CASE
WHEN
v_emp121_sal < v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp121_lname ||
' IS LESS THAN DEPARTMENT AVG');
WHEN
v_emp121_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp121_lname ||
' IS HIGHER THAN AVG DEPARTMENT');
WHEN
v_emp139_sal < v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp139_lname ||
' IS LESS THAN DEPARTMENT AVG');
WHEN
v_emp139_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp139_lname ||
' IS HIGHER THAN AVG DEPARTMENT');
END CASE;
END;
/
理论,我应该在屏幕上收到 “EMP FRIPP的薪酬高于AVG部门” “EMP SEO的薪酬低于AVG部门”
答案 0 :(得分:3)
case
表达式返回一个值。如果您需要两名员工的单独输出,请使用2个case
表达式,以便每个人都返回自己的值。
CASE
WHEN
v_emp121_sal < v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp121_lname ||
' IS LESS THAN DEPARTMENT AVG');
WHEN
v_emp121_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp121_lname ||
' IS HIGHER THAN AVG DEPARTMENT');
END CASE;
CASE
WHEN
v_emp139_sal < v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp139_lname ||
' IS LESS THAN DEPARTMENT AVG');
WHEN
v_emp139_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('SALARY OF EMP ' || v_emp139_lname ||
' IS HIGHER THAN AVG DEPARTMENT');
END CASE;
您可能还希望为=
添加条件,以便在null
或>
条件失败时避免<
输出。