I'm trying to wrap my head around functions in mySQL and I'm currently making one that checks the column account_description
and it's value to see if the description already exists.
If it does exist already, display a message saying so. However, if the description is not there, display a different message saying that it is not found.
Thanks!
MySQL Code:
DROP FUNCTION IF EXISTS test_glaccounts_description
DELIMITER //
CREATE FUNCTION test_glaccounts_description
(
check_description VARCHAR(50)
)
RETURNS VARCHAR(50)
BEGIN
DECLARE var_check VARCHAR(50);
SELECT
account_description INTO var_check
FROM
general_ledger_accounts
WHERE
account_description = check_description;
IF var_check = check_description THEN
SELECT 'That description already exists.';
ELSEIF var_check != check_description THEN
SELECT 'That description does not exist.';
END IF;
RETURN var_check;
END //
DELIMITER ;
SELECT
test_glaccounts_description(account_description) as 'Check'
FROM
general_ledger_accounts
WHERE
account_description = 'Accounting';