Search code examples
mysqlmysql-udf

Can't get this MySQL UDF to Create


I'm receiving a variety of error codes when I try to create this function, most recently this one: "1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2 "

CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2);
BEGIN
  DECLARE Total Float(10,2);
  SELECT Sum(Amount) INTO Total
  FROM tblFinances
  WHERE email=SearchEmail AND Paid=False ;
  RETURN Total;
END;

Any clues as to what I'm doing wrong?


Solution

  • You need to set a different delimiter during your function definition.

    So something like this:

    DELIMITER $$
    
    CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
    RETURNS FLOAT(10,2)
    BEGIN
      DECLARE Total Float(10,2);
      SELECT Sum(Amount) INTO Total
      FROM tblFinances
      WHERE email=SearchEmail AND Paid=False ;
      RETURN Total;
    END$$
    
    DELIMITER ;