ACCEPT p_1 PROMPT 'Please enter the last name:'
DECLARE
v_last_name VARCHAR2(20) := '&p_1';
v_Hire_date person.hire_date%TYPE;
v_Job_type person.job_type%TYPE;
v_Job_status person.Job_status%TYPE;
v_pilot_type pilot.pilot_type%TYPE;
v_n number;
BEGIN
select count(*) into v_n from person
where UPPER(last_name) = v_last_name;
IF v_n != 1 THEN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_last_name) || ' is not in the table. ');
--ELSE
select UPPER(e.last_name),e.hire_date,e.job_type,e.job_status,
p.pilot_type into v_last_name,v_hire_date,v_Job_type,v_Job_status,v_pilot_type
from person e JOIN pilot p
ON (e.person_id = p.person_id)
order by last_name;
DBMS_OUTPUT.PUT_LINE(RPAD('last_name',10) || RPAD('hire_date',10) ||
RPAD('Job_type', 30) || RPAD('Job_status',10) || RPAD('pilot_type', 20));
ELSE
DBMS_OUTPUT.PUT_LINE('No output!');
END IF;
END;
我已编写此代码以显示员工信息。问题出在IF条件下。它没有显示结果。
结果: 如果我将用户输入的姓氏输入为棕色,则仅显示如下
棕色不在表中。
答案 0 :(得分:1)
“如果我输入用户输入的姓氏为棕色”
你的问题有点含糊不清,但如果你实际上是用小写字母输入名字(字面意思是 brown )那么这个问题很容易诊断出来:
您的WHERE子句将列值强制转换为大写但不是输入:
where UPPER(last_name) = v_last_name
这转换为upper('brown') = 'brown', which will never be true,because
'BROWN'<> “brown'`。
因此,要么选择完全匹配,要么将案例转换应用于双方:
where UPPER(last_name) = upper(v_last_name)
或者,也许你有不止一个叫布朗的人?这也会显示没有记录的消息,这是错误的。
答案 1 :(得分:0)
SET SERVEROUTPUT ON;
ACCEPT p_1 PROMPT 'Please enter the last name:'
DECLARE
v_last_name person.last_name%TYPE := UPPER( '&&p_1' );
v_Hire_date person.hire_date%TYPE;
v_Job_type person.job_type%TYPE;
v_Job_status person.Job_status%TYPE;
v_pilot_type pilot.pilot_type%TYPE;
BEGIN
select e.hire_date,
e.job_type,
e.job_status,
p.pilot_type
into v_hire_date,
v_Job_type,
v_Job_status,
v_pilot_type
from person e
JOIN pilot p
ON (e.person_id = p.person_id)
WHERE UPPER( e.last_name ) = v_last_name;
DBMS_OUTPUT.PUT_LINE(
RPAD(v_last_name,10)
|| RPAD(v_hire_date,10)
|| RPAD(v_Job_type, 30)
|| RPAD(v_Job_status,10)
|| RPAD(v_pilot_type, 20)
);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE( v_last_name || ' is not in the table. ');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE( 'No output!' );
END;
/