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 ;
Related
DELIMITER $$
USE `gym`$$
DROP TRIGGER /*!50032 IF EXISTS */ `goods_input_total_amount-updateon-goods_input_price`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `goods_input_total_amount-updateon-goods_input_price`
AFTER UPDATE ON `goods_input_price`
FOR EACH ROW BEGIN
DECLARE input_price INTEGER;
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=goods_input_quantity*input_price;
END;
$$
DELIMITER ;
I have this error message:
Error Code: 1362 Updating of NEW row is not allowed in after trigger
The message is pretty clear. If you want to update the row, you need a before update trigger:
DELIMITER $$
USE gym$$
DROP TRIGGER `goods_input_total_amount_updateon_goods_input_price`$$
CREATE TRIGGER `goods_input_total_amount_updateon_goods_input_price`
BEFORE UPDATE ON goods_input_price
FOR EACH ROW
BEGIN
DECLARE input_price INTEGER;
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 = goods_input_quantityinput_price;
END; $$
DELIMITER ;
I would be inclined to write this as:
BEGIN
SELECT price_goods_input_price
INTO new.goods_input_total_amount
FROM goods_input_price
WHERE id_goods_input_price = NEW.id_goods_input_price
LIMIT 1;
END; $$
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've been looking at other stackoverflow questions here but cant seem to find my fault. I think its syntax-error related. On what part did I got wrong?
DELIMITER $$
DROP TRIGGER IF EXISTS keisan
CREATE TRIGGER keisan AFTER INSERT ON profitdb
FOR EACH ROW
BEGIN
DECLARE shinAgentPercent;
SET #shinAgentPercent:=`AgentRisk`-`SubAgentRisk`;
SET NEW.`SubAgentProfit` = `Profit`*(`SubAgentRisk`/100);
SET NEW.`AgentProfit` = `Profit`*(#shinAgentPercent/100);
END;
$$
Firstly, you need a closing statement indication after drop ... command.
DROP TRIGGER IF EXISTS keisan $$
Secondly, I suggest you to go with a BEFORE INSERT trigger to set expression values for other columns of the tables.
Example:
DELIMITER $$
DROP TRIGGER IF EXISTS keisan $$
CREATE TRIGGER keisan BEFORE INSERT ON profitdb
FOR EACH ROW BEGIN
SET #shinAgentPercent := NEW.AgentRisk - NEW.SubAgentRisk;
SET NEW.SubAgentProfit := NEW.Profit * ( NEW.SubAgentRisk / 100 );
SET NEW.AgentProfit := NEW.Profit * ( #shinAgentPercent / 100 );
END;
$$
-- now reset the delimiter to defaut
DELIMITER ;
Don't you need an end-of-statement after your DROP TRIGGER?
DROP TRIGGER IF EXISTS keisan$$
I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.
delimiter $$
drop TRIGGER if EXISTS upflttyprateTrig
create TRIGGER upflttyprateTrig
AFTER UPDATE ON flttyprate
FOR EACH ROW BEGIN
INSERT INTO `histflttyprate` (
`flttyprate_Id`,
`flttyprate_Flttyp_Id_Fk`,
`flttyprate_Date_Eff`,
`flttyprate_Date_Ineff`,
`flttyprate_IFR`,
`flttyprate_Single`,
`flttyprate_Multi`,
`flttyprate_Rate_Per_Hr`,
`flttyprate_Night_Surchage`,
`flttyprate_Status`,
`flttyprate_Token`)
VALUES (
NEW.flttyprate_Id,
NEW.flttyprate_Flttyp_Id_Fk,
NEW.flttyprate_Date_Eff,
NEW.flttyprate_Date_Ineff,
NEW.flttyprate_IFR,
NEW.flttyprate_Single,
NEW.flttyprate_Multi,
NEW.flttyprate_Rate_Per_Hr,
NEW.flttyprate_Night_Surchage,
NEW.flttyprate_Status,
NEW.flttyprate_Token);
END$$
is not working why what is actual syntax mysqlversion 5.0
The 'IF EXISTS' clause is not supported in DROP TRIGGER statement.
And add a delimiter after 'DROP TRIGGER...' statement.
EDIT
Read the information about trigger's existance from information_schema.triggers table; then drop trigger. Do it in the application or write a stored procedure.
Example:
DELIMITER $$
CREATE PROCEDURE drop_trigger()
BEGIN
SET #exist = NULL;
SELECT 1
INTO
#exist
FROM
information_schema.triggers
WHERE
trigger_schema = 'test'
AND trigger_name = 'trigger1';
IF #exist IS NOT NULL THEN
DROP TRIGGER test.trigger1;
END IF;
END
$$
DELIMITER ;