Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
DROP PROCEDURE IF EXISTS fn_get_entity_by_username;

DELIMITER $$
CREATE function fn_get_entity_by_username
(
    in in_username     varchar,
    out my_entity       integer
)
RETURNS integer DETERMINISTIC
language SQL
BEGIN
    declare my_entity integer(11);
    select entity
      into my_entity
      from tb_entity
     where username = in_username;

END $$

I keep having syntax error

ERROR 1064 (42000) at line 4: 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 in_username varchar, out my_entity integer ) RETURNS integer DE' at line 3'

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Your problem is that you're mixing CREATE FUNCTION and CREATE PROCEDURE syntaxes.

If you meant it to be a function not a procedure there are several problems:

  1. You should use DROP FUNCTION instead of DROP PROCEDURE
  2. MySQL function support only IN parameters
  3. You should use RETURN clause to return a result of the function
  4. There is no need in a local variable my_entity (BTW if you want for some reason use it in a procedure then you should change its name to disambiguate whether you refer to an OUT parameter or that local variable). You can directly RETURN the value.

That being said your code might look like this

DROP FUNCTION IF EXISTS fn_get_entity_by_username;
CREATE FUNCTION fn_get_entity_by_username
(
    in_username VARCHAR(255)
)
RETURNS INT 
RETURN (SELECT entity
         FROM tb_entity
        WHERE username = in_username);

Note: since it's a one statement function there is no need to change DELIMITER and use BEGIN...END block

Sample usage:

SELECT fn_get_entity_by_username('user2') my_entity;

Sample output:

| MY_ENTITY |
|-----------|
|         1 |

If on the other hand you meant it to be a stored procedure then your code might look like this

DROP PROCEDURE IF EXISTS sp_get_entity_by_username;
CREATE PROCEDURE sp_get_entity_by_username
(
    IN  in_username VARCHAR(255),
    OUT my_entity   INT
)
  SET my_entity = (SELECT entity
                     FROM tb_entity
                    WHERE username = in_username);

Sample usage:

CALL sp_get_entity_by_username('user1', @my_entity);
SELECT @my_entity my_entity;

Sample output:

| MY_ENTITY |
|-----------|
|         1 |

Here is SQLFiddle demo (both for a function and a procedure)


Based on your comments code with BEGIN...END block for a function

DROP FUNCTION IF EXISTS fn_get_entity_by_username;
DELIMITER $$
CREATE FUNCTION fn_get_entity_by_username
(
    in_username VARCHAR(255)
)
RETURNS INT 
BEGIN
  RETURN (SELECT entity
            FROM tb_entity
           WHERE username = in_username);
END$$
DELIMITER ;

For a procedure

DROP PROCEDURE IF EXISTS sp_get_entity_by_username;
DELIMITER $$
CREATE PROCEDURE sp_get_entity_by_username
(
    IN  in_username     VARCHAR(255),
    OUT my_entity       INT
)
BEGIN
  SET my_entity = (SELECT entity
                     FROM tb_entity
                    WHERE username = in_username);
END$$
DELIMITER ;
share|improve this answer
    
Because I don't want to change my code format, is there anyway to change my code with begin and end statement?. –  NoVo Oct 4 '13 at 2:42
    
Of course you can. Just add BEGIN...END. See updated answer. –  peterm Oct 4 '13 at 2:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.