Stored procedures 在存储过程中使用游标
我想使用存储过程和游标查找每个季度的最小值、最大值和平均值。不知道我哪里出了问题Stored procedures 在存储过程中使用游标,stored-procedures,plsql,oracle11g,Stored Procedures,Plsql,Oracle11g,我想使用存储过程和游标查找每个季度的最小值、最大值和平均值。不知道我哪里出了问题 SET SERVEROUTPUT ON CREATE OR REPLACE PROCEDURE seven2 IS CURSOR c1 IS SELECT qtrcode as QTR, cast(MIN(salaryoffered) as decimal(4,2)) as MIN, cast(MAX(salaryoffered)
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE seven2
IS
CURSOR c1 IS
SELECT
qtrcode as QTR,
cast(MIN(salaryoffered) as decimal(4,2)) as MIN,
cast(MAX(salaryoffered) as decimal(4,2)) as MAX,
cast(AVG(salaryoffered) as decimal(4,2)) as AVG
FROM
interview
GROUP BY
qtrcode
ORDER BY
qtrcode
BEGIN
FOR qtrcode in c1
LOOP
dbms_output.put_line(min || ' ' ||max|| ' ' ||avg);
END LOOP;
END;
/
我们可以在上面的查询中添加填充函数吗?如果可以,如何添加?为了示例目的,我创建了一个只有两列的访谈表,并插入了一些虚拟数据,请参见过程中的更改,它现在可以正常工作了
SQL>
SQL> desc interview
Name Null? Type
------------------------------------------------------------------------ -------- -------------------------------
SALARYOFFERED NUMBER(10,2)
QTRCODE VARCHAR2(4)
SQL>
SQL> CREATE OR REPLACE PROCEDURE seven2
2 IS
3 CURSOR c1 IS
4 SELECT
5 qtrcode as QTR,
6 cast(MIN(salaryoffered) as decimal(10,4)) as MIN,
7 cast(MAX(salaryoffered) as decimal(10,4)) as MAX,
8 cast(AVG(salaryoffered) as decimal(10,4)) as AVG
9 FROM
10 interview
11 GROUP BY
12 qtrcode
13 ORDER BY
14 qtrcode ;
15
16 BEGIN
17 FOR qtrcode in c1
18 LOOP
19 dbms_output.put_line('The qtr is ' || qtrcode.qtr ||' The min sal is '|| qtrcode.min || ' The max sal is ' || qtrcode.max|| ' The avg sal is' ||qtrcode.avg);
20 END LOOP;
21 END;
22 /
Procedure created.
SQL> exec seven2;
The qtr is Q1 The min sal is 137.07 The max sal is 964.66 The avg sal is 580.8748
The qtr is Q2 The min sal is 127.79 The max sal is 938.49 The avg sal is 550.52
The qtr is Q3 The min sal is 231.1 The max sal is 992.45 The avg sal is 672.59
The qtr is Q4 The min sal is 103.19 The max sal is 960.13 The avg sal is 431.318
PL/SQL procedure successfully completed.
SQL>
定义“出错”。你有错误吗?如果是,错误是什么?在哪条线上?在我脑海中,你在光标定义之后和开始之前缺少了一个分号,你试图引用局部变量min
、max
和avg
,而不是qtrcode
记录中的元素(即qtrcode.min |“'qtrcode.max…
),您使用内置函数名作为别名,而不是有意义的名称。根据你的数据,你的演员阵容看起来也很可疑,除非你真的确定没有人会得到高于99.99的薪水。