如何在pl-sql中修复此功能

Posted

技术标签:

【中文标题】如何在pl-sql中修复此功能【英文标题】:how to fix this function in pl-sql 【发布时间】:2013-05-22 22:11:27 【问题描述】:

我在 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;

怎么了??

【问题讨论】:

***.com/questions/16703573/…的可能重复 【参考方案1】:

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 块中使用它,将该值选择到变量中,然后根据计数是否为零返回。

【讨论】:

如果我想更新函数以返回工资超出范围的员工的 ID,那是怎么回事??!!

以上是关于如何在pl-sql中修复此功能的主要内容,如果未能解决你的问题,请参考以下文章

如何在pl-sql中解析xml?

如何使用 PL-SQL 在 Oracle 中获取列数据类型

如何修复此 WordPress 功能使其不返回 404 页面?

如何在 PL-SQL 上更改之前获取视图/触发器?

如何修复此功能以处理不同类型的错误捕获

如何通过 PL-Sql 获取数字范围内的数字。?