Search code examples
mysqlfunctionstored-functions

How to make use of variable on a function


I need to make a stored function:

This is my code

SELECT count(Dominio) FROM Thogar WHERE DOMINIO='%'

I need to make a stored function where I will write a letter between (U,C,R) and the function will replace the % in the previous code with the selected letter.

How can I do it? Thanks!

Got it working

CREATE FUNCTION `Buscar`(`param` CHAR(1))
    RETURNS INT
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE res INT;
    SELECT count(Dominio) INTO res FROM Thogar WHERE DOMINIO=param;
    RETURN res;
END

    Call buscar('C')

Solution

  • This should work:

    DROP FUNCTION IF EXISTS myFunc;
    DELIMITER $$
    CREATE FUNCTION myFunc(
        param CHAR(1)
    ) RETURNS INT;
    BEGIN
        DECLARE res INT;
        SELECT count(Dominio) INTO res FROM Thogar WHERE DOMINIO=param;
        RETURN res;
    END;
    $$
    DELIMITER ;