MySQL Function

MySQL Function

Create, call and drop MySQL function syntax and example.

MySQL Create function syntax

CREATE OR REPLACE FUNCTION
function_name(parameter_name datatype)
RETURNS datatype
BEGIN
function_body
END;

Create function example

CREATE OR REPLACE FUNCTION 
function_test(p_id int)
RETURNS TEXT
BEGIN
DECLARE v_name TEXT;
	SELECT name INTO v_name 
	FROM test WHERE ID=p_id;
	RETURN v_name;
END;

Output

FUNCTION FUNCTION_TEST compiled

Call function example

SELECT function_test(1);

Output

eee

Drop function example

DROP FUNCTION function_test;

Output

function FUNCTION_TEST dropped.