PL / SQL中忽略的SQL语句

时间:2013-12-05 11:59:50

标签: sql oracle plsql

所以我正在为oracle db编写一个函数,但是我收到错误,说sql语句被忽略了,所以缺少表达式。

功能:

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;

完整的错误消息:

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 

3 个答案:

答案 0 :(得分:0)

您可能正在寻找的是错误处理,而不仅仅是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;

内部BEGIN / END用于异常处理。只能在BEGIN / END块中处理异常,并且由于在异常后需要执行某些操作,因此需要内部BEGIN / END。另一种选择是

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;

答案 1 :(得分:0)

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;

替代建议:

使用 v _ 或类似的内容为变量添加前缀,以避免出现这类错误。

答案 2 :(得分:0)

正确答案如下:

创建或替换函数getQueueNumber(vstudent IN VARCHAR,vcourse IN CHAR) 返回INT IS    queuePosition INT;    date1 TIMESTAMP;    开始        SELECT waitingDate INTO date1 FROM WaitingCourseStudent WHERE student = vstudent AND course = vcourse;        如果SQL%NOTFOUND那么           queuePosition:= NULL;        其他            SELECT COUNT(*)INTO queuePosition FROM WaitingCourseStudent WHERE waitingDate&lt; = date1;       万一;       RETURN queuePosition;   例外     当no_data_found然后       返回-1;     当别人的话       返回-1; END;