创建部门表
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
填充部门表
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
创建员工表
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
填充员工表
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
问题:基于在编写PL/SQL匿名块时创建的DEPARTMENT和EMPLOYEE表,该匿名块接受用户输入的员工ID,并显示1)员工的姓名、雇佣日期和总薪酬(工资+佣金),2)他/她的经理的姓名、雇用日期和总工资(工资+提成),3)员工工作的部门名称,4)该员工所在部门的员工人数,以及5)该员工工作部门的工资总额(工资+佣金),
到目前为止,我收到了下面的查询,我无法获得经理的姓名、聘用日期和总工资(工资+佣金),我该怎么做
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate EMPLOYEE.HIRE_DATE%TYPE;
BEGIN
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE employee_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('USER INPUT:');
DBMS_OUTPUT.PUT_LINE(v_input_emp_id);
DBMS_OUTPUT.PUT_LINE('----------EMPLOYEE----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE manager_id = v_input_emp_id;
IF v_n != 0 THEN
SELECT employee_name ,salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name , v_pay, v_hiredate
FROM employee
WHERE employee.manager_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
END IF;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
ELSE
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
您应该首先获取employees manager_id,然后查询经理。这对我有效:
SET SERVEROUTPUT ON
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_mangager_id employee.manager_id%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate employee.hire_date%TYPE;
BEGIN
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
dbms_output.put_line(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
,manager_id
INTO v_emp_name
,v_pay
,v_hiredate
,v_mangager_id
FROM employee
WHERE employee_id = v_input_emp_id;
dbms_output.put_line('USER INPUT:');
dbms_output.put_line(v_input_emp_id);
dbms_output.put_line('----------EMPLOYEE----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_mangager_id;
IF v_n != 0 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
INTO v_emp_name
,v_pay
,v_hiredate
FROM employee
WHERE employee.employee_id = v_mangager_id;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
END IF;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
ELSE
dbms_output.put_line('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
/
结果:
USER INPUT:
7610
----------EMPLOYEE----------
NAME: WILSON
HIRE DATE: 2001.12.03
TOTAL PAY: $3,000.00
----------MANAGER----------
NAME: JOST
HIRE DATE: 2001.05.04
TOTAL PAY: $4,500.00
----------MANAGER----------
NAME: JOST
PL/SQL procedure successfully completed