1

我是 PL/SQL 语言的新手,我正在解决一个问题,并正在寻找正确方向的建议。任何帮助将不胜感激!

我有一个 Employee 表和一个 Building 表。
Employee 表由 Employee_ID、Last_Name、First_Name 和 Job(F/P) 全职或兼职组成。Building 表由 Employee_ID 和 Building_location 组成。(这是员工工作的位置)家庭或办公室。

我希望用户从用户输入中输入 last_name。因此,我正在寻找员工的信息:员工姓名、工作(全部或部分员工)和 building_location。例如,如果用户输入 Johnson,我希望我的输出看起来像这样:

Employee Name     Job Position     Building Location
====================================================
Andre Johnson      Part Time        Home Office
Nick Johnson       Full Time        Downtown Office

如果用户输入任何字符串,我想列出为公司工作的所有员工。但是,如果名称在 Employee 表中,例如“Johnson”,则它只显示 Johnson。我需要添加另一个 SELECT 子句吗?

在这一点上,我整理了一些代码来检查员工是否为公司工作。我希望以此为基础。

ACCEPT p_1 PROMPT 'Please enter the last name of the employee:'

DECLARE
v_name       employee.last_name%TYPE := '&p_1';
v_lname      employee.last_name%TYPE;
v_fname      employee.first_name%TYPE;
v_jobpos    employee.job%TYPE;
v_buildtype  building.building_type%TYPE;
v_count      NUMBER;

BEGIN
  SELECT    COUNT(*)
  INTO  v_count
  FROM  employee
  WHERE UPPER(last_name) = UPPER(v_name);

  IF v_count = 0 THEN
        DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME        Job Position        Building Location');
        DBMS_OUTPUT.PUT_LINE('----------------------------------------------------'  );
        for i in (select * from employee order by last_name) loop
    DBMS_OUTPUT.PUT_LINE(i.last_name || ', ' || i.first_name)
 end loop;

ELSE
    for i in (select * from employee order by last_name) loop
        DBMS_OUTPUT.PUT_LINE(i.last_name || ', ' || i.first_name);

    end loop; 

END IF;
END;
4

1 回答 1

0

You can do this. The simplest way might be like this:

    ...
    IF v_count = 0 THEN
        DBMS_OUTPUT.PUT_LINE(v_name || ' Does not work here.');
        /* New code starts here! */
        DBMS_OUTPUT.PUT_LINE('Employees are:');
        for r in (select * from employees) loop
            dbms_output.put_line(r.last_name||', '||r.first_name);
        end loop;
        /* New code ends here! */
    ELSE
      ...
    end if;
end;
于 2013-05-09T15:13:05.383 回答