我正在尝试编写PL / SQL函数名称hoursWorked,它接受workerId和prjId并返回worker在项目中工作的总小时数。
表名为PROJECTHOURS
WORKERPRJID | WORKERID | PRJID | TOTHOURS | CDATE
---------- ---------- ---------- ---------- ---------
1 1 1 10 1-JAN-14
2 1 1 7 2-JAN-14
3 1 1 6 4-JAN-14
4 2 1 5 11-JAN-14
5 2 1 9 15-JAN-14
6 2 1 7 13-JAN-14
7 1 2 5 11-JAN-14
8 2 2 9 15-JAN-14
9 2 2 7 13-JAN-14
我尝试了以下内容:
CREATE OR replace PROCEDURE Hoursworked (j IN NUMBER,
n IN NUMBER)
IS
hours_worked projecthours.tothours%TYPE := 0;
BEGIN
SELECT workerid,
prjid,
SUM(tothours)
INTO hours_worked
FROM projecthours
WHERE workerid = j
AND prjid = n
GROUP BY j,
n;
RETURN hours_worked;
END;
SQL> show errors;
Errors for PROCEDURE HOURSWORKED:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
6/52 PL/SQL: ORA-00947: not enough values
12/1 PL/SQL: Statement ignored
12/1 PLS-00372: In a procedure, RETURN statement cannot contain an
expression
不可否认,我对SQL一般都缺乏经验,所以任何见解都非常受欢迎,关于我做错了什么。
答案 0 :(得分:2)
选择错误,您选择3个值,但只获取一个。按引用分组不正确。
您不需要选择workerid或项目ID,也不需要分组。
应该如下
CREATE OR replace PROCEDURE Hoursworked (j IN NUMBER,
n IN NUMBER,
hours_worked OUT NUMBER)
IS
BEGIN
SELECT SUM(tothours)
INTO hours_worked
FROM projecthours
WHERE workerid = j
AND prjid = n;
END;
在程序中,Return立即结束程序执行,并且不期望任何参数。将程序单元类型更改为函数,或使用上述OUT变量。
请提供比" j"更好的参数名称。 &安培; " N"