Skip to content
Advertisement

Error #1064 in creating function in MySQL

friends.

I am trying to create function in MySQL using this script:

CREATE FUNCTION CurrentMemoDepartment(MID INT)
RETURNS INT
BEGIN
  DECLARE DeptID INT;
  SET DeptID = 0;
  SELECT TOP 1 TDeptID INTO DeptID FROM Transactions WHERE MemoID = MID ORDER BY ID DESC;
  RETURN DeptID;
END

but it says:

1064 – 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 ” at line 4

Advertisement

Answer

Single-statement function does not need in BEGIN-END, intermediate variables, DELIMITER reassing.

And MySQL uses LIMIT, not TOP n.

CREATE FUNCTION CurrentMemoDepartment(MID INT)
RETURNS INT
RETURN SELECT TDeptID 
       FROM Transactions 
       WHERE MemoID = MID 
       ORDER BY ID DESC
       LIMIT 1;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement