I'm creating this procedure which should return a VARCHAR value as output. But I keep getting
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 'BEGIN SELECT mgrType FROM Manager INTO mgType WHERE empNo=@eID; END
I have chosen // as new DELIMITER character so it is not an issue with delimiter. Here's my code.
DELIMITER //
CREATE PROCEDURE returnEmpTy( IN un VARCHAR(40), IN pw VARCHAR(60), OUT mgType VARCHAR(30) )
BEGIN
DECLARE eID INT;
SELECT empNo INTO eID FROM Employee WHERE username=un AND pssWrd=pw;
IF EXISTS(SELECT @eID FROM Manager)
BEGIN
SELECT mgrType FROM Manager INTO mgType WHERE empNo=@eID;
END
END//
DELIMITER ;
edit: resolved!
There are several problems in your code, but none of them have anything to do with the delimiter.
One problem is that
SELECT ... INTO
allows theINTO
right beforeFROM
, or at the end of the query, but not where you used it beforeWHERE
. Review the examples in the manual: https://dev.mysql.com/doc/refman/8.0/en/select-into.htmlAnother is that you use
@eID
for the variableeID
. These are not the same variable in MySQL.Another is that you use
IF
withoutTHEN
orEND IF
and you have no;
terminator anyway. You should refer the syntax and examples for MySQL's IF statement.Another is that
IF/THEN/END IF
doesn't need aBEGIN
block (unless you need to useDECLARE
inside the block).Your method of checking if the row exists in Manager is not going to do what you think. The EXISTS will return true if there are any rows in that table.
You don't need to check if there are rows in the table anyway. Just do the
SELECT ... INTO
. It'll assign NULL tomgType
if there are no matching rows.You could simplify this further by using
JOIN
instead of a local variable: