2

I have been trying to create this stored procedure in phpMyAdmin (MySQL) and it's giving me an error, which I have stated below. I have two variables, that is advance and paid. it's to get values and subtract the loan the employee left to pay from what he has already paid for, to return what is left to pay. I have tried everything, but I can't find the error. I need help, please.

CREATE PROCEDURE getadvancebalance( IN e int, d date) 
BEGIN  

DECLARE advance float(50);
DECLARE paid    float(50);

SELECT SUM(Amount) INTO advance
FROM vw_employee_advances
WHERE (entity_id = @e) AND (start_date <= @d) AND (approve_status = 'Approved')
IF (advance is null) THEN SET advance := 0; END IF;

SELECT SUM(Amount) INTO paid
FROM vw_advance_deductions
WHERE (entity_id = @e) AND (start_date <= @d);
IF (paid is null) THEN SET paid := 0; END IF;

advance := advance - paid;

RETURN advance;
END 

This is the error, i am getting: MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4

1 Answers1

1

You have a few syntax errors, you should do as this:

DELIMITER //
CREATE function getadvancebalance( e int, d date) returns integer
BEGIN  
  DECLARE advance float(50);
  DECLARE paid    float(50);

  SELECT SUM(Amount) INTO advance
  FROM vw_employee_advances
  WHERE (entity_id = e) AND (start_date <= d) AND (approve_status = 'Approved');

  IF (advance is null) THEN 
      SET advance = 0; 
  END IF;

  SELECT SUM(Amount) INTO paid
  FROM vw_advance_deductions
  WHERE (entity_id = e) AND (start_date <= d);

  IF (paid is null) THEN 
     SET paid = 0; 
  END IF;

  set advance = advance - paid;

  RETURN advance;
END //
DELIMITER ;

Tested in MySql 5.6.*

Some notes, use meaningful names for your variables (e and d are not); don't use the @ in the parameters you seem to mix the local query variable with the function/procedure parameters; Since you return something it should be a function rather then a procedure.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87