0

I'm using xampp with phpMyAdmin to manage a MySql database. When creating a function I got a "No Return Found" error, so I stripped down the function to narrow the origin of the error I got to the simplest case where it still doesn't work.

Code

And the error message is this:

Error message

Apparently if the RETURN statement isn't the only thing on the code I get an error.

Martin
  • 16,093
  • 1
  • 29
  • 48
  • What about starting *definition* by `BEGIN` and ending it with `END` ? – Cid Nov 28 '19 at 14:13
  • @Cid Yeah, I realized that. For some reason I thought phpMyAdmin would do that automatically – Lince Assassino Nov 28 '19 at 14:16
  • PHPMyAdmin's routine generator is a bit problematic as it requires you to structure your desired code inputting differently to if you are writing the SQL yourself from scratch. – Martin Nov 28 '19 at 14:23
  • Try this: `Defeinition: "BEGIN SET @c = 0; RETURN @c; END"` – Martin Nov 28 '19 at 14:24

2 Answers2

3

Assuming the actual code being executed against MySQL is:

CREATE FUNCTION `Contagem`() RETURNS INT(11)
SET @c = 0;
RETURN @c;

This will fail, because you have not wrapped the function in BEGIN...END.

Try instead to write this as your function declaration:

BEGIN
    SET @c = 0;
    RETURN @c;
END;

Alternatively, declare the function yourself directly in MySQL:

DELIMITER $$

CREATE FUNCTION `Contagem`() RETURNS INT(11)
BEGIN
    SET @c = 0;
    RETURN @c;
END $$

DELIMITER ;
Martin
  • 16,093
  • 1
  • 29
  • 48
1

When reviewing this post I was reading more carefully the error message I realized the MySql code to define the function didn't have BEGIN and END in it.

I assumed when typing function code with several lines that phpMyAdmin would know how to handle it, since the code text box has support to multiple lines of code.

So, putting BEGIN at the beginning and END at the end of the code solved my problem.