当我执行这个PL/SQL块时,我总是收到“找不到数据”。如果我注释掉输出行,那么所有的输出都会像我所怀疑的那样输出,但是更新语句从不执行。有任何想法吗?Oracle PL/SQL选择和更新
set serveroutput on;
Declare
TYPE type_emp IS RECORD(
emp_salary emp.salary%TYPE);
rec_emp type_emp;
v_stars varchar2(50) ;
v_count number(3);
s_num number(3);
begin
SELECT min(employee_id) into s_num from emp;
SELECT count(*) into v_count from emp;
v_count := v_count + s_num;
for i in s_num .. v_count loop
SELECT salary
into rec_emp
from emp
where employee_id = i;
if rec_emp.emp_salary <1000 then
v_stars := null;
elsif rec_emp.emp_salary >=1000 and rec_emp.emp_salary <2000 then
v_stars := '*';
elsif rec_emp.emp_salary >=2000 and rec_emp.emp_salary <3000 then
v_stars := '**';
elsif rec_emp.emp_salary >=3000 and rec_emp.emp_salary <4000 then
v_stars := '***';
elsif rec_emp.emp_salary >=4000 and rec_emp.emp_salary <5000 then
v_stars := '****';
elsif rec_emp.emp_salary >=5000 and rec_emp.emp_salary <6000 then
v_stars := '*****';
elsif rec_emp.emp_salary >=6000 and rec_emp.emp_salary <7000 then
v_stars := '******';
elsif rec_emp.emp_salary >=7000 and rec_emp.emp_salary <8000 then
v_stars := '*******';
elsif rec_emp.emp_salary >=8000 and rec_emp.emp_salary <9000 then
v_stars := '********';
elsif rec_emp.emp_salary >=9000 and rec_emp.emp_salary <10000 then
v_stars := '*********';
elsif rec_emp.emp_salary >=10000 and rec_emp.emp_salary <11000 then
v_stars := '**********';
elsif rec_emp.emp_salary >=11000 and rec_emp.emp_salary <12000 then
v_stars := '***********';
elsif rec_emp.emp_salary >=12000 then
v_stars := '************';
end if;
--dbms_output.put_line(rec_emp.emp_salary || ' ' || i || ' '|| v_stars);
update emp set emp.stars = v_stars where employee_id = i;
end loop;
end;
除了杰弗里的答案,你可以简化所有的'elsif'语句是这样的:'如果rec_emp。 emp_salary <1000 then v_stars:= null;否则v_stars:= lpad('*',至少(floor(rec_emp.emp_salary/1000),12),'*');结束如果;'。 – 2012-02-07 03:42:04