Dembele Dembele - 6 months ago 9
SQL Question

MySQL, create a simple function

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?

Answer

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;//
Comments