mysqldatabasestored-proceduresphpmyadminmysql-error

"Error #1313 RETURN is only allowed in a FUNCTION" When Creating a Stored Procedure in phpMyAdmin


I'm trying to create the following stored procedure in phpMyAdmin. But, I keep getting this error: #1313 - RETURN is only allowed in a FUNCTION

CREATE PROCEDURE IS_POINT_IN_POLYGON()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE pX DECIMAL(19,16);
DECLARE pY DECIMAL(19,16);
DECLARE ls LINESTRING;
DECLARE poly1 POINT;
DECLARE poly1X DECIMAL(19,16);
DECLARE poly1Y DECIMAL(19,16);
DECLARE poly2 POINT;
DECLARE poly2X DECIMAL(19,16);
DECLARE poly2Y DECIMAL(19,16);
DECLARE i INT DEFAULT 0;
DECLARE result INT(1) DEFAULT 0;
SET pX = X(p);
SET pY = Y(p);
SET ls = ExteriorRing(poly);
SET poly2 = EndPoint(ls);
SET poly2X = X(poly2);
SET poly2Y = Y(poly2);
SET n = NumPoints(ls);
WHILE i<n DO
SET poly1 = PointN(ls, (i+1));
SET poly1X = X(poly1);
SET poly1Y = Y(poly1);
IF ( ( ( ( poly1X <= pX ) 
&& ( pX < poly2X ) ) || ( ( poly2X <= pX ) 
&& ( pX < poly1X ) ) ) 
&& ( pY > ( poly2Y - poly1Y ) * ( pX - poly1X ) / ( poly2X - poly1X ) + poly1Y ) ) THEN
SET result = !result;
END IF;
SET poly2X = poly1X;
SET poly2Y = poly1Y;
SET i = i + 1;
END WHILE;
RETURN result;
END//

How do I solve this problem? In the SQL pane, I remembered to change the delimiter to // .

Any suggestions?


Solution

  • The error message tells you exactly what the problem is - you can't use RETURN in a PROCEDURE. Create a FUNCTION instead (CREATE FUNCTION), which can return a value.

    CREATE FUNCTION <FunctionName> RETURNS <DataType>