MySQL Create Function Example

First of all we have to crate our example table called “Customer”.

We have to insert one record into Customer table. Let’s add Steve Jobs! 🙂

Here is syntax to create our function which will bring Concat First and Last Name and return single string.

How to call getCustomerFullName() function.

This is how our output will look.

getCustomerFullNameOutput

4 thoughts on “MySQL Create Function Example

  1. Please give some difference between functions and procedures.

  2. please tell If i can return multiple values using a function, say all fields of a comment table join to all fields of user table.

  3. other way in the line 5 of the function.

    CREATE DEFINER=scott@%
    FUNCTION mysql.getCustomerFullName(intCustomerID INT)
    RETURNS varchar(100) CHARSET latin1
    BEGIN
    DECLARE strCustomerFullName VARCHAR(100);
    SET strCustomerFullName = (SELECT CONCAT(FirstName, ‘ ‘, LastName) FROM Customer WHERE CustomerID = intCustomerID);
    return strCustomerFullName;
    END

  4. Thanks Gustavo, your query is more efficient than mine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.