49

I want to create a simple MySQL function, I have this MySQL procedure:

CREATE PROCEDURE getUser(gU INT)
   SELECT * FROM Company
   WHERE id_number = gU;

CALL getUser(2);

I need some help making this into a MySQL function. What are the pros and cons of using a function over a procedure?

4
  • stored procedures can do most anything you want. they're simply a way of encapsulating a lot of logic behind a simple name, basically the same as in any programming language. they don't HAVE to select/update/whatever. When you say "function", do you mean a UDF? Commented Jan 30, 2013 at 15:00
  • Oh I see. I don't know what UDF means but by function I mean a stored function, in MySQL. Syntax is like CREATE FUNCTION etc. Commented Jan 30, 2013 at 15:08
  • MariaDB: CREATE FUNCTION fFullname ( id INT(12) ) RETURNS CHAR(100) CHARACTER SET 'utf8' COLLATE 'utf8_bin' DETERMINISTIC RETURN ( SELECT fullname FROM table_user WHERE user_id = id ); Commented Nov 4, 2022 at 9:53
  • See MySQL CREATE FUNCTION Syntax Commented May 12, 2023 at 18:20

3 Answers 3

50

this is a mysql function example. I hope it helps. (I have not tested it yet, but should work)

DROP FUNCTION IF EXISTS F_TEST //
CREATE FUNCTION F_TEST(PID INT) RETURNS VARCHAR
BEGIN
/*DECLARE VALUES YOU MAY NEED, EXAMPLE:
  DECLARE NOM_VAR1 DATATYPE [DEFAULT] VALUE;
  */
  DECLARE NAME_FOUND VARCHAR DEFAULT "";

    SELECT EMPLOYEE_NAME INTO NAME_FOUND FROM TABLE_NAME WHERE ID = PID;
  RETURN NAME_FOUND;
END;//
Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting a syntax error on ' DECLARE NAME_FOUND VARCHAR DEFAULT ""; ' What is DEFAULT ""? I assume NAME_FOUND is a variable you've made?
DEFAULT "" is a default value for NAME_FOUND which is the variable I use to store the result of the query. It is also optional and not mandatory.
Where the comment is. ' NOM_VAR1 DATATYPE [DEFAULT] ' do I need to fill anything in there, or I can delete that? Because ATM there's syntax error in the NAME_FOUND part, IDK why.
@zameeramir to call a function just use "select function_name(parameter);" in this case it woud be "select F_TEST(345);" where 345 would be the employee id as an example.
I am getting a syntax error.
|
21

MySQL function example:

Open the mysql terminal:

el@apollo:~$ mysql -u root -pthepassword yourdb
mysql>

Drop the function if it already exists

mysql> drop function if exists myfunc;
Query OK, 0 rows affected, 1 warning (0.00 sec)

Create the function

mysql> create function hello(id INT)
    -> returns CHAR(50)
    -> return 'foobar';
Query OK, 0 rows affected (0.01 sec)

Create a simple table to test it out with

mysql> create table yar (id INT);
Query OK, 0 rows affected (0.07 sec)

Insert three values into the table yar

mysql> insert into yar values(5), (7), (9);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

Select all the values from yar, run our function hello each time:

mysql> select id, hello(5) from yar;
+------+----------+
| id   | hello(5) |
+------+----------+
|    5 | foobar   |
|    7 | foobar   |
|    9 | foobar   |
+------+----------+
3 rows in set (0.01 sec)

Verbalize and internalize what just happened:

You created a function called hello which takes one parameter. The parameter is ignored and returns a CHAR(50) containing the value 'foobar'. You created a table called yar and added three rows to it. The select statement runs the function hello(5) for each row returned by yar.

Comments

-4

Try to change CREATE FUNCTION F_TEST(PID INT) RETURNS VARCHAR this portion to CREATE FUNCTION F_TEST(PID INT) RETURNS TEXT

and change the following line too.

DECLARE NAME_FOUND TEXT DEFAULT "";

It should work.

1 Comment

Right. this worked for me. in fact it seems VARCHAR is not a valid type, VARCHAR(50) or TEXT are.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.