I have goods_input and goods_input_price tables and want to calculate a total amount when inserting a new row in goods_input.
Trigger is the following:
DELIMITER $$
USE `gym`$$
DROP TRIGGER /*!50032 IF EXISTS */ `total`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `total` BEFORE INSERT ON `goods_input`
FOR EACH ROW BEGIN
SET new.goods_input_total_amount=new.goods_input_quantity*goods_input_price.`price_goods_input_price`
WHERE goods_input.`id_goods_input_price`=goods_input_price.`id_goods_input_price`;
END;
$$
DELIMITER ;
This is the error message:
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 'where
goods_input.id_goods_input_price=goods_input_price.`id_goods_input_price' at line 6
The set statement does not have a where clause. If you want data from another table, then you need to fetch them via a select into variables in the trigger.
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `total` BEFORE INSERT ON `goods_input`
FOR EACH ROW BEGIN
DECLARE input_price integer; /*use whatever datatype you have in your db for the price */
SELECT price_goods_input_price INTO input_price FROM goods_input_price
WHERE id_goods_input_price=NEW.id_goods_input_price LIMIT 1;
SET new.goods_input_total_amount=new.goods_input_quantity*input_price;
END$$
Related
I am trying to delete the 10 million records from DB and more with DELETE procedure of SQL. I want to delete the records in loop like with each interaction record should delete 1 like below.
drop procedure if exists PROC_WEEKLY_TASK_DELETE;
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `PROC_WEEKLY_TASK_DELETE`()
BEGIN
set #rows = (select count(*) from MY_TABLE where IS_VALID_DATE='false');
set #a = 0;
while(#a<#rows)
do delete from MY_TABLE where IS_VALID_DATE='false' limit 1;
set #a = (#a+1);
commit;
end while;
END
//
DELIMITER ;
I am using above procedure to delete the record but getting problem like syntax other like
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 'DELIMITER' at line 1
Please help me to create the procedure to delete the record in loop. Thanks
Thanks for your all suggestions specially #Akina
Please check my new Procedure which worked for me to delete the records.
drop procedure if exists PROC_WEEKLY_TASK_DELETE_LIMIT;
DELIMITER $$
CREATE DEFINER=root#localhost PROCEDURE PROC_WEEKLY_TASK_DELETE_LIMIT()
BEGIN
repeat delete from MY_TABLE where IS_VALID_DATE = 'false' limit 10000;
until not row_count()
end repeat;
END$$
DELIMITER ;
Above procedure runs successfully on local but in case of server you need to remove the DEFINER only
I'm trying to create a BEFORE INSERT trigger and getting errors that I don't understand.
The MySQL to create the trigger looks like this:
CREATE TRIGGER `GetPolyLinkID` BEFORE INSERT ON `TableA`
FOR EACH ROW
BEGIN
INSERT INTO TableB () VALUES ();
NEW.PolyLinkID = last_insert_id();
END
The error I'm getting is
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
You forgot SET ... begore NEW.PolyLinkID = last_insert_id();
And
For creating complex trigger , you must use the delimiter keyword .
This is a example from rom https://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html .
delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END;
//
delimiter ;
Screen shot with sequel pro
DELIMITER $$
CREATE OR REPLACE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `goods_input_total_amount-updateon-goods_input` BEFORE UPDATE
ON `gym`.`goods_input`
FOR EACH ROW BEGIN
DECLARE input_price INTEGER; /*use whatever datatype you have in your db for the price */
SELECT price_goods_input_price INTO input_price FROM goods_input_price
WHERE id_goods_input_price=NEW.id_goods_input_price LIMIT 1;
SET new.goods_input_total_amount=new.goods_input_quantity*input_price;
END$$
DELIMITER ;
The syntax is working for "before insert" trigger but I got this error message:
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 'TRIGGER
goods_input_total_amount-updateon-goods_input BEFORE UPDATE
ON `gy' at line 3
CREATE OR REPLACE TRIGGER is not there in MySQL
Try
DELIMITER $$
CREATE
TRIGGER `goods_input_total_amount-updateon-goods_input` BEFORE UPDATE
ON `gym`.`goods_input`
FOR EACH ROW BEGIN
DECLARE input_price INTEGER; /*use whatever datatype you have in your db for the price */
SELECT price_goods_input_price INTO input_price FROM goods_input_price
WHERE id_goods_input_price=NEW.id_goods_input_price LIMIT 1;
SET new.goods_input_total_amount=new.goods_input_quantity*input_price;
END$$
DELIMITER ;
If you want to delete an existing TRIGGER, use
DROP TRIGGER IF EXISTS Trigger_name
before the create trigger code
I have faced this problem when I created an after insert trigger. I know this is an syntax error, but I don't know how to fix it in my SQL.
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 5
Here is my trigger:
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET #v_UserID := (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END
You need to have a delimiter
delimiter //
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET v_UserID = (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END;//
delimiter ;
I am trying to fetch some values from php page and perform trigger in mysql database. Which is as below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger` AFTER INSERT OR UPDATE ON `table2`
FOR EACH ROW BEGIN
IF(NEW .flag=0) THEN
INSERT INTO temp(sent,pcount,ncount) VALUES (NEW.sent,NEW.pcount,NEW.ncount);
ELSE
UPDATE temp SET pcount=NEW.pcount AND ncount=NEW.ncount WHERE id = NEW.id;
END IF;
END;
$$
DELIMITER ;
Which gives error:
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 'or update ON `table2`
FOR EACH ROW BEGIN
if(NEW .flag=0) then
INSERT INTO' at line 3
I tried to change = with =: but error persist. Any idea whaat the prob?
You can't define a trigger for update and insert at the same time. You have to define 2 triggers:
CREATE TRIGGER `Mysql_insert_Trigger` AFTER INSERT ON `table2` ...
CREATE TRIGGER `Mysql_update_Trigger` AFTER UPDATE ON `table2` ...
I count do it. There was problem in syntax. Here is the correct which perform update insert at same moment in different table and update-inset on same table but with if else:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger2`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger2` AFTER INSERT ON `table2`
FOR EACH ROW BEGIN
IF 'flag'=1 THEN
UPDATE record SET sent=NEW.sent WHERE id=NEW.id;
ELSE
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp VALUES ()
END IF;
END;
$$
DELIMITER ;