0

我在 pl-sql 中编写了一个函数来检查所有员工的工资是否在正确的最高和最低工资范围之间。但它给了我这样的错误:错误(9,3):PL/SQL:语句被忽略。错误(9,16):PLS-00306:调用“>”时参数的数量或类型错误

CREATE OR REPLACE FUNCTION MIN_MAX_SAL RETURN NUMBER AS 
cursor emp_cur is select salary from employees ;
emp_sal emp_cur%rowtype;
min_sal jobs.min_salary%type;
max_sal jobs.max_salary%type;
BEGIN
select min_salary , max_salary into min_sal , max_sal from jobs;
for emp_sal in emp_cur loop
if ((emp_sal > max_sal) or (emp_sal < min_sal)) then
return 0;
end if;
end loop;
RETURN 1;
END MIN_MAX_SAL;

怎么了??

4

1 回答 1

0

emp_sal是一种行类型,即使它只包含一列。您需要指定要比较的列:

if ((emp_sal.salary > max_sal) or (emp_sal.salary < min_sal)) then

您不需要emp_sal使用这种形式的游标循环进行声明。

如果 中有多个记录jobs,这似乎很可能,那么您首先选择会抛出ORA-02112, 选择了太多行。您可能的意思是将每位员工与其工作的薪水范围进行比较。假设您有意在 PL/SQL 中将其作为练习并希望使用游标——因为它可以很容易地在单个 SQL 语句中完成——你可能想要使用嵌套循环,一个循环来查找工作信息,然后内部循环检查所有从事该工作的员工,例如:

create or replace function min_max_sal return number as
    cursor job_cur is
        select job_id, min_salary, max_salary from jobs;
    cursor emp_cur(p_job_id jobs.job_id%type) is
        select emp_id, salary from employees where job_id = p_job_id;
begin
    for job_rec in job_cur loop
        for emp_rec in emp_cur(job_rec.job_id) loop
            if (emp_rec.salary > job_rec.max_salary)
                or (emp_rec.salary < job_rec.min_salary) then
                return 0;
            end if;
        end loop;
    end loop;
    return 1;
end min_max_sal;
/

当然,您可以一键加入:

select count(*)
from employees e
join jobs j on j.job_id = e.job_id
where (e.salary > j.max_salary)
or (e.salary < j.min_salary);

或者:

where not e.salary between j.min_salary and j.max_salary

如果您愿意,您仍然可以在 PL/SQL 块中使用它,将该值选择到变量中,然后根据计数是否为零来返回。

于 2013-05-22T22:40:14.083 回答