[英]SQL statements ignored in PL/SQL
So i'm writing a function for oracle db but I get errors saying that the sql statements were ignored and so expressions are missing. 所以我正在为oracle db写一个函数,但是我收到错误消息说sql语句被忽略了,所以表达式丢失了。
The function: 功能:
CREATE OR REPLACE FUNCTION getQueueNumber
(student IN VARCHAR,course IN CHAR)
RETURN INT
IS
queuePosition INT;
date TIMESTAMP;
BEGIN
SELECT waitingDate INTO date
FROM WaitingCourseStudent wcs
WHERE wcs.student=student AND wcs.course=course;
IF SQL%NOTFOUND THEN queuePosition := NULL;
ELSE
SELECT COUNT(*) as pos
INTO queuePosition
FROM WaitingCourseStudent
WHERE waitingDate<=date;
END IF;
RETURN queuePosition;
END;
Full error message: 完整的错误消息:
LINE/COL ERROR
8/5 PL/SQL: SQL Statement ignored
8/29 PL/SQL: ORA-00936: missing expression
13/9 PL/SQL: SQL Statement ignored
16/20 PL/SQL: ORA-00936: missing expression
What you're probably looking for, is error handling instead of just an if. 您可能正在寻找的是错误处理,而不只是if。
CREATE OR REPLACE FUNCTION getQueueNumber
(p_student IN VARCHAR, p_course IN CHAR)
RETURN INT
IS
queuePosition INT;
v_date TIMESTAMP;
BEGIN
BEGIN
SELECT waitingDate INTO v_date
FROM WaitingCourseStudent wcs
WHERE wcs.student = p_student AND wcs.course = p_course;
EXCEPTION
WHEN NO_DATA_FOUND THEN RETURN NULL;
END;
SELECT COUNT(*) as pos
INTO queuePosition
FROM WaitingCourseStudent
WHERE waitingDate <= v_date;
RETURN queuePosition;
END;
The inner BEGIN/END is for the exception handling. 内部的BEGIN / END用于异常处理。 An exception can only be handled within a BEGIN/END block and since you needed to do something after your exception, you need an inner BEGIN/END. 异常只能在BEGIN / END块内处理,并且由于您需要在异常之后做一些事情,因此您需要一个内部BEGIN / END。 An alternative would be 一个替代方案是
CREATE OR REPLACE FUNCTION getQueueNumber
(p_student IN VARCHAR, p_course IN CHAR)
RETURN INT
IS
queuePosition INT;
v_date TIMESTAMP;
BEGIN
SELECT waitingDate INTO v_date
FROM WaitingCourseStudent wcs
WHERE wcs.student = p_student AND wcs.course = p_course;
SELECT COUNT(*) as pos
INTO queuePosition
FROM WaitingCourseStudent
WHERE waitingDate <= v_date;
RETURN queuePosition;
EXCEPTION
WHEN NO_DATA_FOUND THEN RETURN NULL;
END;
SELECT waitingDate INTO date <--- (date is reserved keyword for denoting oracle datatype of date)
FROM WaitingCourseStudent wcs
WHERE wcs.student=student AND wcs.course=course;
IF SQL%NOTFOUND THEN queuePosition := NULL;
Alternate suggestion : 替代建议:
Prefix your variables with v_ or something like that to avoid these kind of errors. 给变量加上v_或类似的前缀,以免发生此类错误。
The correct answer is as follows: 正确答案如下:
CREATE OR REPLACE FUNCTION getQueueNumber (vstudent IN VARCHAR,vcourse IN CHAR) RETURN INT IS queuePosition INT; 创建或替换函数getQueueNumber(vstudent in VARCHAR,vcourse in CHAR)返回INT是queuePosition INT; date1 TIMESTAMP; date1 TIMESTAMP; BEGIN SELECT waitingDate INTO date1 FROM WaitingCourseStudent WHERE student=vstudent AND course=vcourse; 从WaitingCourseStudent那里选择SELECT WaitingDate到date1那里,学生= vstudent和课程= vcourse; IF SQL%NOTFOUND THEN queuePosition := NULL; 如果SQL%NOTFOUND THEN queuePosition:= NULL; ELSE SELECT COUNT(*) INTO queuePosition FROM WaitingCourseStudent WHERE waitingDate <= date1; 其他选择COUNT(*)进入queuePosition from WaitingCourseStudent WHEREwaitingDate <= date1; END IF; 万一; RETURN queuePosition; RETURN queuePosition; exception when no_data_found then return -1; 当no_data_found然后返回-1时发生异常; when others then return -1; 当其他人返回-1时; END; 结束;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.