admin管理员组文章数量:1335400
the function needs to get the average from a single row, which are the grades from a student.
Also, it needs to handle the errors, insert them on another table called ERRORES
to archive them. I want to use the SQLCODE
and SQLERRM
, but it doesn't let me. It gives me the following errors:
Error(22,9): PL/SQL: SQL Statement ignored
Error(23,42): PL/SQL: ORA-00984: column not allowed here
The table ERRORES
was created with this parameters:
CREATE TABLE ERRORES
(id_error NUMBER(10) NOT NULL,
subprograma_error VARCHAR2(50) NOT NULL,
descripcion_error VARCHAR2(500),
PRIMARY KEY (id_error));
This is the Function:
CREATE OR REPLACE FUNCTION FN_PROMEDIO(cod_asig nota_alumno.cod_asignatura%TYPE)
RETURN NUMBER IS
promedio nota_alumno.nota1%TYPE := 0;
BEGIN
SELECT
ROUND((nota1 + nota2 + nota3 + nota4 + nota5) / 5, 1)
INTO
promedio
FROM
nota_alumno
WHERE
cod_asignatura = cod_asig;
RETURN
promedio;
EXCEPTION
WHEN OTHERS THEN
INSERT INTO ERRORES
VALUES(
SEQ_ERRORES.NEXTVAL,
'Error en función "FN_PROMEDIO',
TO_CHAR(SQLCODE) || ' - ' || SQLERRM);
END;
What should I do? Are the error messages to long for ERRORES
?
the function needs to get the average from a single row, which are the grades from a student.
Also, it needs to handle the errors, insert them on another table called ERRORES
to archive them. I want to use the SQLCODE
and SQLERRM
, but it doesn't let me. It gives me the following errors:
Error(22,9): PL/SQL: SQL Statement ignored
Error(23,42): PL/SQL: ORA-00984: column not allowed here
The table ERRORES
was created with this parameters:
CREATE TABLE ERRORES
(id_error NUMBER(10) NOT NULL,
subprograma_error VARCHAR2(50) NOT NULL,
descripcion_error VARCHAR2(500),
PRIMARY KEY (id_error));
This is the Function:
CREATE OR REPLACE FUNCTION FN_PROMEDIO(cod_asig nota_alumno.cod_asignatura%TYPE)
RETURN NUMBER IS
promedio nota_alumno.nota1%TYPE := 0;
BEGIN
SELECT
ROUND((nota1 + nota2 + nota3 + nota4 + nota5) / 5, 1)
INTO
promedio
FROM
nota_alumno
WHERE
cod_asignatura = cod_asig;
RETURN
promedio;
EXCEPTION
WHEN OTHERS THEN
INSERT INTO ERRORES
VALUES(
SEQ_ERRORES.NEXTVAL,
'Error en función "FN_PROMEDIO',
TO_CHAR(SQLCODE) || ' - ' || SQLERRM);
END;
What should I do? Are the error messages to long for ERRORES
?
1 Answer
Reset to default 2In the SQLCODE
documentation it states:
A SQL statement cannot invoke
SQLCODE
.
And the SQLERRM
documentation states similar:
A SQL statement cannot invoke
SQLERRM
.
Instead, assign SQLCODE
and SQLERRM
to a variable and use the variable in the SQL statement, rather than trying to directly use them:
CREATE OR REPLACE FUNCTION FN_PROMEDIO(
cod_asig nota_alumno.cod_asignatura%TYPE
) RETURN NUMBER IS
promedio nota_alumno.nota1%TYPE := 0;
v_error NUMBER;
v_errm VARCHAR2(1000);
BEGIN
SELECT ROUND((nota1 + nota2 + nota3 + nota4 + nota5) / 5, 1)
INTO promedio
FROM nota_alumno
WHERE cod_asignatura = cod_asig;
RETURN promedio;
EXCEPTION
WHEN OTHERS THEN
v_error := SQLCODE;
v_errm := SQLERRM;
INSERT INTO ERRORES (
id_error,
subprograma_error,
descripcion_error
) VALUES(
SEQ_ERRORES.NEXTVAL,
'Error en función "FN_PROMEDIO',
TO_CHAR(v_error) || ' - ' || v_errm
);
RETURN NULL;
END;
/
However, you may want to move the logging into an autonomous transaction and re-raise the exception:
CREATE PROCEDURE log_error(
i_error NUMBER,
i_errm VARCHAR2
)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO ERRORES (
id_error,
subprograma_error,
descripcion_error
) VALUES(
SEQ_ERRORES.NEXTVAL,
'Error en función "FN_PROMEDIO',
TO_CHAR(i_error) || ' - ' || i_errm
);
COMMIT;
END;
/
CREATE OR REPLACE FUNCTION FN_PROMEDIO(
cod_asig nota_alumno.cod_asignatura%TYPE
) RETURN NUMBER IS
promedio nota_alumno.nota1%TYPE := 0;
v_error NUMBER;
v_errm VARCHAR2(1000);
BEGIN
SELECT ROUND((nota1 + nota2 + nota3 + nota4 + nota5) / 5, 1)
INTO promedio
FROM nota_alumno
WHERE cod_asignatura = cod_asig;
RETURN promedio;
EXCEPTION
WHEN OTHERS THEN
log_error(SQLCODE, SQLERRM);
RAISE;
END;
/
fiddle
本文标签: sqlError in SELECT clause present in a EXCEPTION functionStack Overflow
版权声明:本文标题:sql - Error in SELECT clause present in a EXCEPTION function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386798a2465197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论