1

I'm creating a stored function like this

CREATE FUNCTION getVendorID(IN venname VARCHAR(255)) 
RETURNS INT
BEGIN
    DECLARE a INT;
    SELECT vendorid FROM vendors WHERE vendorname LIKE venname INTO a;
    RETURN a;
END$$

but I receive an error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN venname VARCHAR(255))
RETURNS INT
BEGIN
DECLARE a INT;
SELECT vendorid FRO' at line 1

3 Answers 3

3

MySQL functions only takes IN-parameters, and therefor they cannot be declared as IN.

DELIMITER $$
CREATE FUNCTION getVendorID(venname VARCHAR(255)) 
RETURNS INT
BEGIN
    DECLARE a INT;
    SELECT vendorid INTO a FROM vendors WHERE vendorname LIKE venname;
    RETURN a;
END$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

2
  1. no IN for functions
  2. INTO after select list, before FROM
  3. READS SQL DATA to avoid binary log issues

So your function definition should look like:

DELIMITER $$
DROP FUNCTION IF EXISTS getVendorID$$
CREATE FUNCTION getVendorID( venname VARCHAR(255) ) 
RETURNS INT
READS SQL DATA
BEGIN
  DECLARE a INT;
  SELECT vendorid INTO a FROM vendors WHERE vendorname LIKE venname;
  RETURN a;
END$$
DELIMITER ;

5 Comments

SQL reads do not affect the binary log, SQL writes do!, still + 1 though.
READS SQL DATA is needed to tell mysql function won't write anything
The official docs says: "CONTAINS SQL indicates that the routine does not contain statements that read or write data. This is the default if none of these characteristics is given explicitly. Examples of such statements are SET @x = 1 or DO RELEASE_LOCK('abc'), which execute but neither read nor write data." Notice the this is the default part.
I agree with you that it's a good idea to put these statements in but this particular one is not needed for the binary log, deterministic and nondeterministic are needed for the log though. This one is nondeterministic, from the MySQL docs: "If binary logging is enabled, the DETERMINISTIC characteristic affects which routine definitions MySQL accepts. See Section 17.6, “Binary Logging of Stored Programs”."
From the same section: By default, for a CREATE FUNCTION statement to be accepted, at least one of DETERMINISTIC, NO SQL, or READS SQL DATA must be specified explicitly.
1

Why all that code? Use this:

CREATE FUNCTION getVendorID(IN venname VARCHAR(255)) 
RETURNS INT
BEGIN
    RETURN (SELECT vendorid FROM vendors WHERE vendorname LIKE venname LIMIT 1);
END$$

Also note introduction od LIMIT 1. Your code will explode if more than one vendor matches; you can't put the vendorid from multiple rows into one variable.

You may consider auto-wrapping with % as a service to your callers: WHERE vendorname LIKE CONCAT('%', venname, '%')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.