问题
I'm using Aurora Serverless MySQL 5.6 to create the following trigger which will update one table when data is inserted into another table but am receiving syntax errors, specifically around the Delimiter keyword.
DELIMITER $$
CREATE TRIGGER Create_Media_Like_Trigger AFTER INSERT ON MediaLike
FOR EACH ROW
BEGIN
IF NEW.likeType = 'LIKE' THEN
UPDATE Media
SET Media.numLikes = Media.numLikes + 1
WHERE Media.mediaId = NEW.mediaId;
ELSEIF NEW.likeType = 'DISLIKE' THEN
UPDATE Media
SET Media.numLikes = Media.numLikes - 1
WHERE Media.mediaId = NEW.mediaId;
ENDIF;
END $$
DELIMITER ;
Like I said above, I am receiving syntax errors near DELIMITER
, is this issue AWS specific and how can I fix it?
UPDATE with error messages:
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 'DELIMITER $$ CREATE TRIGGER Create_Media_Like_Trigger
AFTER INSERT ON MediaLike ' at line 1
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 'ELSEIF NEW.likeType = 'DISLIKE' THEN UPDATE Media SET
Media.numLikes = Media.num' at line 1
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 'END $$' at line 1
回答1:
I spent a day trying to figure this out so hopefully this helps someone out there...
DELIMITER
is a feature of the client, not the MySQL server. The RDS Query Editor is a client but it does not support changing the delimiter so attempting to run the script you've provided won't work since the first time it sees a semicolon it will interpret that as the end of the command and fail with a syntax error.
So, how do you create something like a stored procedure that has multiple statements and semicolons in it? You must create it as an .sql
file and send it using the Data API from either a Lambda function or the CLI.
First, create your script in a .sql
file without any DELIMITER
commands or alternate delimiters.
For example: function.sql
CREATE PROCEDURE simpleproc (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END
Then, run the script using the CLI like this:
cat function.sql | xargs -0 aws rds-data execute-statement \
--resource-arn arn:aws:rds:eu-west-1:xxx:cluster:cluster-name \
--secret-arn arn:aws:secretsmanager:eu-west-1:xxx:secret:secret-name-xxx \
--database "database_name" \
--sql
Alternatively, you can create a Lambda function that reads the file and uses rds_client.execute_statement()
to send the script to the server via the Data API. But again, do NOT use the DELIMITER
statement. The server sees the BEGIN
and END
lines and acts accordingly without the need to change the delimiter.
回答2:
This info may not strictly be relevant to the OP's question, but it's closely related and may help someone else in a similar situation (ie. from a search).
I was running a similar CREATE FUNCTION
query on an Amazon Aurora Serverless MySQL 5.6 database, and was getting the same syntax errors. I was using Table Plus (v2.9.1, build 264) as my application/client.
The solution was to delete the 2x DELIMITER
lines. The application/client was intelligent enough to work out the delimiters itself.
来源:https://stackoverflow.com/questions/56660118/syntax-error-when-using-delimiters-with-aurora-serverless-mysql-5-6