问题
This has an error next to VARCHAR2
and (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;
Here it says there is an error next to the decleration of 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:
For the Employee_exists
function, change this:
RETURN VARCHAR2(4)
To this:
RETURN VARCHAR
For the Calculate_bonus
function, move this line...
v_years_service NUMBER;
... so it's under the AS
line:
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
Finally, there's this line:
v_years_service = TRUNC(MONTHS_BETWEEN(SYSDATE, v_startdate) /12);
The variable assignment is always :=
, so change it to this:
v_years_service := TRUNC(MONTHS_BETWEEN(SYSDATE, v_startdate) /12);
Addendum A Also note the comment from @JoeW under your question. Joe states - correctly - that your ELSE
condition will never be hit.
Addendum B
Here's the full function. You may also want to round the return value, as a salary multiplied by 0.02 or 0.05 can give fractional cents. To round, substitute RETURN ROUND(p_bonus, 2);
for 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;
/
来源:https://stackoverflow.com/questions/19140714/these-bits-of-code-all-have-errors-in-how-can-i-fix-this