PL / SQL - 精确提取返回超过请求的行数

时间:2016-01-18 09:48:02

标签: sql oracle plsql

我正在尝试创建一个函数,该函数将返回部门内每个职位的最高工资和最高工资的订单。

消息必须是:

部门:部门名称,

工作/职位:工作名称,最高工资:工资金额,

create or replace PACKAGE BODY Salary AS

FUNCTION max_sal(DEPTNO_F NUMBER)
RETURN VARCHAR2 IS  
dept_name VARCHAR2(25); 
job_possition VARCHAR(25);
maximum_salary NUMBER;
message VARCHAR2(255);

BEGIN



SELECT DNAME, JOB, MAX(SAL) AS "SAL" 
INTO  job_possition, maximum_salary
FROM EMP
WHERE DEPTNO = DEPTNO_F
GROUP BY JOB, DNAME
ORDER BY SAL DESC;


message := 'Department name: '||dept_name|| 'Job positin: '   ||job_possitin||, 'Maximum Salary: ' ||maximum_salary;

return message;



END max_sal;

END Salary;

2 个答案:

答案 0 :(得分:0)

问题是您的查询返回的行不止一行而且无法选择2个变量。您是否尝试在包外部运行SELECT SQL语句并检查了要返回的行?

对于任何给定的DEPTNO_F值,您不应该有多个DNAMEJOB的排列

答案 1 :(得分:0)

要在查询结果中处理多行,您必须使用循环:

create or replace PACKAGE BODY Salary AS

FUNCTION max_sal(DEPTNO_F NUMBER)
RETURN VARCHAR2 IS  
dept_name VARCHAR2(25); 
job_possition VARCHAR(25);
maximum_salary NUMBER;
message VARCHAR2(255);

BEGIN


for i in (SELECT DNAME, JOB, MAX(SAL) SAL 
            FROM EMP
           WHERE DEPTNO = DEPTNO_F
           GROUP BY JOB, DNAME
           ORDER BY SAL DESC) loop

  message := message || 'Department name: ' || i.DNAME || ', Job position: ' ||
             i.JOB || ', Maximum Salary: ' || i.SAL || chr(10);

end loop;
return message;

END max_sal;

END Salary;