无法创建此存储过程。有什么问题

时间:2017-12-28 14:27:42

标签: mysql stored-procedures

我一直在尝试在phpMyAdmin(MySQL)中创建这个存储过程,它给了我一个错误,我在下面说过。我有两个变量,即提前和支付。这是为了获得价值并减去员工从他已支付的款项中支付的贷款,以退还剩余的款项。我已经尝试了一切,但我找不到错误。我需要帮助。

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 

这是错误,我得到:MySQL说:文档#1064 - 你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行的''附近使用正确的语法

1 个答案:

答案 0 :(得分:1)

你有一些语法错误,你应该这样做:

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 ;

在MySql 5.6。*

中测试

一些注意事项,为变量使用有意义的名称(e和d不是);不要在参数中使用@,而是将本地查询变量与函数/过程参数混合使用;因为你返回的东西应该是一个函数而不是一个过程。