Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL how do I create a stored procedure that takes multiple parameters?

Example of function: call getThings(amount, place, limit, marginError)

SYNOPSIS: CALL getThings(4, PA, 3, 1.2);

example of goal:

CREATE PROCEDURE getThings(IN amount X, place VARCHAR(30), lim INT, marginError double)
SELECT place, limit, amount
FROM AREA, PRODUCT
WHERE AREA.place=PRODUCT.place
AND PRODUCT.AREA=place
ORDER BY ABS(AMOUNT-marginError)
LIMIT lim;
END

Desired goal is to retrieve the closest 3 products from a stored procedure (using MySQL) but I keep getting sytax errors in trying to create the procedure.

like image 858
stackoverflow Avatar asked Feb 07 '12 18:02

stackoverflow


2 Answers

since you didn't post the exact error/message,

EDIT: I assume you are missing the IN/OUT for the 2.and 3. parameter. - Not true, see comments.

e.g.

DELIMITER$$
CREATE PROCEDURE getThings(IN amount X, IN place VARCHAR(30), IN lim INT)
   SELECT place, `limit`, amount
   FROM AREA, PRODUCT
   WHERE AREA.place=PRODUCT.place
   AND PRODUCT.AREA=place
   ORDER BY ABS(AMOUNT-5)
   LIMIT lim;
END$$
DELIMITER;
like image 139
Bernhard Kircher Avatar answered Nov 09 '22 20:11

Bernhard Kircher


LIMIT is MySQL's reserved word. If you really need to use it as column name, put in in backticks (``). Also, your paramteres have same names as columns in your table, which adds to confusion.

like image 25
Mchl Avatar answered Nov 09 '22 20:11

Mchl