这些代码都有错误我怎么解决这个问题?

时间:2013-10-02 15:29:27

标签: sql oracle plsql syntax-error

VARCHAR2(4)旁边有错误:

CREATE OR REPLACE FUNCTION Employee_exists
 (p_employee_id IN NUMBER)
RETURN VARCHAR2(4);
AS
BEGIN
        SELECT employee_id
        FROM employees
        WHERE employee_id = p_employee_id;

        RETURN 'true';

END Employee_exists;

这里说明v_years_service

的删除旁边有错误
CREATE OR REPLACE FUNCTION Calculate_Bonus(p_salary IN NUMBER, p_startdate IN DATE)
RETURN NUMBER -- here it errors
AS
  v_years_service NUMBER;
BEGIN

  v_years_service := TRUNC(Months_Between(SYSDATE, v_startdate) /12);

  IF (v_years_service <2) THEN
        p_bonus := v_salary * 0.02;
  ELSE 
  IF (v_year_service <2) THEN
        p_bonus := v_salary * 0.04;
  ELSE
        p_bonus := v_salary * 0.05;

  RETURN p_bonus;
END IF;
END Calculate_Bonus;
/

show err

1 个答案:

答案 0 :(得分:1)

对于Employee_exists功能,请更改此项:

RETURN VARCHAR2(4)

对此:

RETURN VARCHAR

对于Calculate_bonus函数,请移动此行...

v_years_service NUMBER;

...所以它在AS行下:

CREATE OR REPLACE FUNCTION Calculate_bonus(p_salary IN NUMBER, p_startdate IN DATE)
RETURN NUMBER
AS
  v_years_service NUMBER; --here is where the error appears
BEGIN
  ... and the rest

最后,还有这一行:

v_years_service = TRUNC(MONTHS_BETWEEN(SYSDATE, v_startdate) /12);

变量赋值始终为:=,因此请将其更改为:

v_years_service := TRUNC(MONTHS_BETWEEN(SYSDATE, v_startdate) /12);

附录A 还请注意@JoeW在您提问时的评论。乔正确地说 - 你的ELSE条件永远不会被击中。


附录B

这里有完整的功能。您可能还想舍入返回值,因为工资乘以0.02或0.05可以给出小数分数。要进行舍入,请将RETURN ROUND(p_bonus, 2);替换为RETURN p_bonus;

CREATE OR REPLACE FUNCTION Calculate_Bonus(p_salary IN NUMBER, p_startdate IN DATE)
RETURN NUMBER
AS
  v_years_service NUMBER;
  v_bonus NUMBER;
BEGIN
  v_years_service := TRUNC(Months_Between(SYSDATE, p_startdate) /12);
  IF (v_years_service <2) THEN
        v_bonus := p_salary * 0.02;
  ELSE
        v_bonus := p_salary * 0.05;
  END IF;
  RETURN v_bonus;
END Calculate_Bonus;
/