I am trying to make a database that does some inventory checking, and I am working on some triggers to update everything as required. In this case, that I want to do is to take the current availability (disponibilidad) of an ingredient from the ingredients table, add to it how much is taken or added, and then save that value into the table stock_disponibilidad, as you can see from the trigger code I am trying to use.
DELIMITER $$
CREATE TRIGGER `update_disponibilidad_variacion` BEFORE INSERT ON `stock_disponibilidad`
FOR EACH ROW BEGIN
DECLARE old_disp DOUBLE DEFAULT 0;
SELECT disponibilidad INTO old_disp FROM ingredientes WHERE id = NEW.ingredientes_id;
NEW.disponibilidad = old_disp + NEW.variacion;
END$$
DELIMITER ;
However, whenever I execute the query to create the trigger, I get the following error:
#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 '.disponibilidad = old_disp + NEW.variacion; END' at line 5
I've done everything I read about this issue, still without result. What am I doing wrong?
Related
This is my first time trying to create a MySql trigger but I'm running into syntax errors that I can't identify. I am trying to have the trigger insert a calculated value when a row is updated. Below is my code, but I keep getting syntax errors when I try to execute it, and I cannot see where the error is. Can someone please look, what am I doing wrong?
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END;
Here is the error I get in MySQL Workbench, but it's not much help. :/
Error Code: 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 5
MySQL Workbench highlights the end of line 6 as the start of the syntax error.
As said by #Akina I was missing the delimiter statements. Here is fixed code.
delimiter //
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END//
delimiter ;
As per the advice of #Akina I read the Create Procedure documentation from the dev.mysql.com site and the below quote highlighted the importance of using delimiter
The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. See Section 25.1, “Defining Stored Programs”.
I'm trying to create a trigger to import a field from another table when a new row is inserted but get the error message:
#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 'END' at line 6
I'm using the sql tab in phpmyadmin to enter the code.
I've read all the related questions on here, perused several other sites with articles on mysql triggers but can't find anything that helps.
I've tried with // at the end of the line prior to END//
I've tried with ; at the end of the line prior to END//
Nothing seems to work.
My trigger code:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy))
END//
DELIMITER ;
Both towns2 and world_map tables exist and have the required columns.
As far as I can tell this should fetch the RegionID field from any row in the world_map table that has X & Y fields matching the new mapx and mapy columns but all I can get is this error.
No doubt I've got a really simple error in this somewhere but for the life of me I can't find it and all help is greatfully recieved.
UPDATE
Re-created the code as shown by #Chris J below in the myphpadmin trigger form, ignored the big red X against the END statement and hit the GO button - it worked so I guess the problem was not with my code but with my chosen method of creating the trigger. The code as created by phpmyadmin is shown below:
CREATE TRIGGER `towns2_before_insert` BEFORE INSERT ON `towns2`
FOR EACH ROW BEGIN
SET NEW.regionid =
(Select RegionID from world_map
where (X = new.mapx) AND (Y = new.mapy));
END
Although you've mentioned it already, the following works perfectly for me:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy));
END//
This is based on a guess of your schema from your query. The trigger now exists and is visible in Heidi as follows:
I know this question has been asked a bunch in different forms, but none of the ones I looked at (quite a bit) seemed to help me out in my specific case. I wrote a few functions and procedures and I always get the same error at the same spot. Here is my code:
DELIMITER |
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId, Name FROM STUDENT WHERE Status = sts;
END |
DELIMITER;
This happens on all my procedures functions, this error:
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 'CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId,' at line 2
On my other one its this:
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 'CREATE FUNCTION GoodGrade(letGrade VARCHAR(2)) RETURNS int
BEGIN
DECLARE v' at line 2
This happens whether or not I use | or // as the DELIMITER...can someone tell me what I'm doing wrong here? Thanks!
You've changed the delimiter, written a statement and then not delimited it according to your new definition
Change the line
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
to
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS |
Update
This wasn't obvious from the initial error message, but you have another error when you reset the Delimiter after the procedure definition. You need a space before the ';' on that one.
so change
DELIMITER;
to
DELIMITER ;
NB
The other use of ';' within your procedure definition is correct, because you want everything within the create statement to to be processed together.
Here i am trying to insert into payment_table after updating the review_table only after the status in review_table is updated to 2 .But there is error after running this trigger i.e,
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the
Trigger is:
CREATE TRIGGER payment_details_trig AFTER UPDATE ON review_table
FOR EACH ROW
BEGIN
IF NEW.status=2
INSERT INTO payment_table(paper_id, payment_mode, payment_refNo, payment_image,status)
VALUES (0,0,0,0,0);
END IF;
END;
Nothing sticks out except for the missing THEN. You might need delimiter statements:
DELIMITER $$
CREATE TRIGGER payment_details_trig AFTER UPDATE ON review_table
FOR EACH ROW
BEGIN
IF NEW.status = 2 THEN
INSERT INTO payment_table(paper_id, payment_mode, payment_refNo, payment_image,status)
VALUES (0, 0, 0, 0, 0);
END IF;
END;
$$
DELIMITER ;
The issue 1064 in SQL comes in following four cases:
1) Use of reserved words:
Every version of MySQL has its own list of reserved words. These are words that are used for specific purposes or perform specific functions within the MySQL engine. If you attempt to use one of these reserved words, you will receive the 1064 error.To fix the issue, you will want to surround the word with backticks “`”.
2) Missing Data:
Sometimes data in the database is missing. This can cause issues when this data is required for a query.
3) Mistyped or missing Commands:
One of the most common causes for the 1064 error is when a SQL statement uses a mistyped or missing command.
4) Deprecated Commands:
Some commands that were deprecated (slated for removal but still allowed for a period of time) eventually go obsolete. This means that the command is no longer valid in the SQL statement.
In your case third option is the answer is the reason of your error. You have missed writing THEN after IF.
I am trying to make a procedure to update an existing user. It receives the name of the user and then increments his points column. I have it like this:
CREATE PROCEDURE addPoints (IN nomeus varchar(20))
BEGIN
UPDATE User
SET
points=points+1
WHERE (nome=nomeus) ;
END;
However, I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 8
How can I fix it?
When in doubt just go to dev.mysql they have some great documentation there.
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Looks like you found the answer to your own question. However, if you change any of the code in the procedure, don't forget to drop the procedure before trying to create it again.
Drop Procedure if exists addPoints;
http://dev.mysql.com/doc/refman/5.0/en/drop-procedure.html
Yeah, I had to use delimiters:
delimiter //
CREATE PROCEDURE addPoints(IN nomeuser varchar(256))
BEGIN
UPDATE User
SET
points = points + 1
WHERE nome = nomeuser;
END;//
delimiter ;