Can't get this MySQL UDF to Create

54 Views Asked by At

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?

2

There are 2 best solutions below

0
ydaetskcoR On BEST ANSWER

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 ;
0
user3751672 On

Solution was a combo of needing a different delimiter and removing ; after RETURNS FLOAT(10,2)

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 ;

Thanks!