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
Related
I have below trigger code but It give me error. I can't figure out what is wrong with my code.
DROP TRIGGER IF EXISTS `user_has_voice_queues_rt_update`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `user_has_voice_queues_rt_update`
BEFORE UPDATE ON `user_has_voice_queues_rt`
FOR EACH ROW begin
if(new.pause='0') then
Set new.penalty = (select max(penalty) from user_has_voice_queues_rt) + 1;
end if;
Try this:
DROP TRIGGER IF EXISTS `user_has_voice_queues_rt_update`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
TRIGGER `user_has_voice_queues_rt_update`
BEFORE UPDATE ON `user_has_voice_queues_rt`
FOR EACH ROW begin
if(new.pause='0') then
Set new.penalty = (select max(penalty) from user_has_voice_queues_rt) + 1;
end if;
END $$
DELIMITER ;
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$$
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): 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 UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;
I'm trying to create trigger in Mysql, and get the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TRIGGER `foo`.`test` AFTER INSERT ON `foo`.`test` FOR EACH ROW BE' at line 2
in this trigger:
DELIMITER $$
SET #name2:='w';
CREATE TRIGGER `foo`.`test`
AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SELECT name1 INTO name2;
END;
$$
DELIMITER ;
What is wrong with line 2?
try this:
DELIMITER $$
CREATE TRIGGER `foo`.`test` AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SET #name2:='w';
SELECT name1 INTO #name2;
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 ;