CREATE FUNCTION db.scalar_func
(
@a AS INT,
@b AS INT
)
RETURNS INT -- return type
AS
BEGIN
RETURN @a @b -- return statement
END;
I was trying the above code to test user defined functions however it states expecting an identifier. Anyone has any idea how to resolve it?
CodePudding user response:
Remove every AS
, they don't belong. Function variables/parameters don't have @
in front of them. You need a ;
at the end of the RETURN statement.
Wherever you are learning function syntax is clearly not correct or you are not reading carefully enough.
CodePudding user response:
It is unnecessary to use delimiter because the function definition contains no internal ; statement delimiters:
mysql> CREATE FUNCTION scalar_func (a int ,b int)
-> RETURNS INT DETERMINISTIC
-> RETURN a b ;
Query OK, 0 rows affected (0.05 sec)
mysql> select scalar_func(10,12);;
--------------------
| scalar_func(10,12) |
--------------------
| 22 |
--------------------
1 row in set (0.00 sec)