I want to create the following function in Mysql, but the function does not get created but fails with an error
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE @AccountType varchar(50);
SELECT AccountType INTO @AccountType
FROM AccountType
WHERE AccountTypeID = AccountTypeID);
RETURN AccountType;
END $$
DELIMITER ;
A description of my table
CREATE TABLE AccountType(
AccountTypeID INT AUTO_INCREMENT PRIMARY KEY,
AccountType varchar(100) UNIQUE NOT NULL
);
The error I am getting is
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '@AccountType varchar(50);
I can’t seem to find out what I am doing wrong. Can someone please help.
This should work:
This will also work:
The first example uses a “global” variable (those starting with an ‘@’ symbol). The second example uses a “local” variable.