Skip to main content

Examples of how to create a User-defined Function and a Stored Procedure in MySQL.

--
-- To create a User-defined Function in MySQL 5.7x:
--
-- Usage:
--
--   SELECT myfunc('world');
--

drop function if exists myfunc;
create function myfunc(suffix varchar(20))
    returns varchar(50) deterministic
    begin
        return CONCAT('Hello, ', suffix, '!');
    end;

--
-- To create a Stored Procedure in MySQL 5.7x:
--
-- Usage:
--
--   CALL myproc();
--

drop procedure if exists myproc;
create procedure myproc()
    begin
        select *
        from mytablename;
    end;