在 Oracle 中创建函数时出现 PLS-00103 错误
Posted
技术标签:
【中文标题】在 Oracle 中创建函数时出现 PLS-00103 错误【英文标题】:Getting PLS-00103 error when creating a function in Oracle 【发布时间】:2012-11-28 03:51:55 【问题描述】:我在以下函数中遇到了几个错误:
CREATE OR REPLACE FUNCTION department_salary
(emp_dept VARCHAR2, salary NUMBER)
RETURN NUMBER AS
dept_sal NUMBER := salary;
BEGIN
IF emp_dept = 'MIS' THEN dept_sal := (dept_sal * .10) + dept_sal;
ELSEIF emp_dept = 'SALES' THEN dept_sal := (dept_sal * .15) + dept_sal;
ELSEIF emp_dept = 'HR' THEN dept_sal := (dept_sal * .20) + dept_sal;
ELSE dept_sal := -1;
END IF;
RETURN dept_sal;
END;
/
我得到的错误如下:
LINE/COL ERROR
-------- ----------------------------------------------------------------
7/10 PLS-00103: Encountered the symbol "EMP_DEPT" when expecting one
of the following:
:= . ( @ % ;
8/10 PLS-00103: Encountered the symbol "EMP_DEPT" when expecting one
of the following:
:= . ( @ % ;
14/4 PLS-00103: Encountered the symbol ";" when expecting one of the
following:
if
【问题讨论】:
【参考方案1】:问题在于IF
语句的语法。特别是,关键字是ELSIF
而不是ELSEIF
(不是e
)。如果你改变它,函数将编译
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE FUNCTION department_salary
2 (emp_dept VARCHAR2, salary NUMBER)
3 RETURN NUMBER
4 AS
5 dept_sal NUMBER := salary;
6 BEGIN
7 IF emp_dept = 'MIS' THEN dept_sal := (dept_sal * .10) + dept_sal;
8 ELSIF emp_dept = 'SALES' THEN dept_sal := (dept_sal * .15) + dept_sal;
9 ELSIF emp_dept = 'HR' THEN dept_sal := (dept_sal * .20) + dept_sal;
10 ELSE dept_sal := -1;
11 END IF;
12 RETURN dept_sal;
13* END;
14 /
Function created.
但是,如果您正在编写这样的代码,通常使用CASE
语句会更清晰
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE FUNCTION department_salary
2 (emp_dept VARCHAR2, salary NUMBER)
3 RETURN NUMBER
4 AS
5 dept_sal NUMBER := salary;
6 BEGIN
7 CASE emp_dept
8 WHEN 'MIS' THEN dept_sal := (dept_sal * .10) + dept_sal;
9 WHEN 'SALES' THEN dept_sal := (dept_sal * .15) + dept_sal;
10 WHEN 'HR' THEN dept_sal := (dept_sal * .20) + dept_sal;
11 ELSE dept_sal := -1;
12 END CASE;
13 RETURN dept_sal;
14* END;
15 /
Function created.
【讨论】:
像return case emp_dept when 'mis' then foo when 'sales' then bar
等这样编码可能会更清楚。以上是关于在 Oracle 中创建函数时出现 PLS-00103 错误的主要内容,如果未能解决你的问题,请参考以下文章