I can't update my table with trigger in mysql - mysql

I'm trying to fill the empty field in the table after I update that table but I am get this error :
#1442 - Can't update table 'services_offer' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
How can I fix it?
my trigger's query:
DELIMITER $$
Create trigger OfferSum
after UPDATE
on Services_Offer
FOR EACH ROW
BEGIN
UPDATE Services_Offer
SET NEW.final_price =
(SELECT service_price from Services_Level
where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size;
END$$

I think you want a before update trigger:
DELIMITER $$
CREATE TRIGGER OfferSum BEFORE UPDATE ON Services_Offer
FOR EACH ROW
BEGIN
SELECT NEW.final_price := sl.service_price * NEW.service_size
FROM Services_Level sl
WHERE sl.level_code = NEW.level_code;
END$$

Why do you need a trigger for this? It looks like the query above will result in and endless loop. Every time you update a row, it calls an update trigger which actually updates the table and thus triggers the trigger again and again and again until something crashes.
Instead, why don't you calculate and update the column withing the initial update. The query would look like this:
UPDATE Services_Offer
SET
column1 = value1,
column2 = value2,
final_price =
(
SELECT service_price
FROM Services_Level
WHERE Services_Level.level_code = Services_Offer.level_code
) * service_size
WHERE id = XXX
where column1 = value1, column2 = value2 comes from your initial update query.

Related

MariaDB move data to another table with TRIGGER

i want to move my current data to another table when the update value is "jakarta"
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1
SELECT * FROM detail_mhs WHERE id_mhs = NEW.id_mhs;
DELETE FROM detail_mhs WHERE id_mhs = OLD.id_mhs;
END IF;
but with this trigger its make error like this
1442 - Can't update table 'detail_mhs' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
any solutions?
Please try below trigger and change col1 & col2 with your column's name and change primary key with column name which have primary key and not updating.
DELIMITER $$
CREATE TRIGGER detail_mhs_trig
AFTER UPDATE ON detail_mhs FOR EACH ROW
BEGIN
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1(col1,col2, alamat)
VALUES(old.col1, old.col2, new.alamat);
END IF; END$$
DELIMITER ;

sum trigger on insert Mysql

I have a table called sum_test with id,a,b,c fields
I need a trigger that calculates the sum of a+b on c but Im new to triggers.
How can I do that on Mysql?
CREATE TRIGGER `example` AFTER INSERT ON `sum_test`
FOR EACH ROW BEGIN
UPDATE sum_test
SET c= (a+b)
WHERE id = id
END
If we are wanting to set column c on the row we are inserting, then we can do like this, in a BEFORE insert trigger:
DELIMITER $$
CREATE TRIGGER `sum_test_bi` BEFORE INSERT ON `sum_test`
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b ;
END$$
DELIMITER ;
I don't understand the reference to table suma. What does that table have to do with anything? We note that the condition id = id is going to evaluate to TRUE for every row in the table where id IS NOT NULL.

MySQL trigger for updating another table on updating value in current table

I have two tables A and B in mysql database. Table A having brnd_id, R_2000ml. Table B having diff,val. I am writing a trigger to update table B when R_2000ml is updated. I am calculating the difference of NEW.R_2000ml and OLD.R_2000ml and going to store the result in diff column and passing a boolean value as 1 to get updated in val column of table B.
Below is my trigger code which is not working as the values are not getting updated in table B.
DELIMITER //
DROP TRIGGER IF EXISTS `myTrigger`;
CREATE DEFINER=`root`#`localhost` TRIGGER `myTrigger`
AFTER UPDATE ON `A`
FOR EACH ROW
BEGIN
DECLARE brndid integer;
DECLARE stkdate date;
if NEW.`R_2000ml` <=> OLD.`R_2000ml`
THEN
if NEW.`R_2000ml` > OLD.`R_2000ml`
THEN
update B set diff= NEW.R_2000ml- OLD.R_2000ml,val=1;
END IF;
END IF;
END;//
DELIMITER ;
In your if clause you use "<=>", This operator performs an equality comparison like the = operator, so you should use <> or !=:
if NEW.`R_2000ml` <> OLD.`R_2000ml`
or
if NEW.`R_2000ml` != OLD.`R_2000ml`

Can I update the just added row using MySQL triggers

The default initial value of one column in my database is the same as the row's auto-incremented id. I'm trying to use triggers to set it.
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END
But this keeps throwing a syntax 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 '' at line 5
I've tried all sorts of permutations of this with no luck. Can anyone see what I'm doing wrong?
As zerkms said, you need to change the delimeter. But since you only use 1 line of code, you don't need the BEGIN and END. And that way, you don't need to change the delimiter either
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
Since you are getting an error you cannot update the row, I suggest the following:
Do NOT perform the update query at all. On default the order value = the ID value. So when the order value changes, you can update it properly.
If you are requesting the data with php, do something like this:
$order = $row['order'];
if ($order == '')
$order = $row['id'];
After you need it updating, you've got the correct value.
I don't think you can do that. An AFTER INSERT trigger cannot modify the same table, neither by issuing an UPDATE nor by something like this:
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
SET NEW.`order` = NEW.id ;
which results in this error:
> Error Code: 1362. Updating of NEW row is not allowed in after trigger
You can't either use a BEFORE INSERT trigger because then the NEW.id is not known (if you modify the above, the order column will get 0 value after the Insert.
What you can do, is use a transaction:
START TRANSACTION ;
INSERT INTO clusters (id)
VALUES (NULL);
UPDATE clusters
SET `order` = id
WHERE id = LAST_INSERT_ID();
COMMIT ;
You get the error because mysql treats ; in line 5 as the end of your trigger declaration, which obviously leads to the syntax error.
So you need to redefine delimiter before you specify the trigger body:
delimiter |
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END;
|
delimiter ;
You can create just BEFORE INSERT TRIGGER, it's works like this:
CREATE TRIGGER `default_order_value`
BeFORE INSERT ON `clusters`
FOR EACH ROW
BEGIN
SET NEW.`order` = NEW.id ;
END
same as below we are using
DELIMITER $$
USE `e_store`$$
DROP TRIGGER /*!50032 IF EXISTS */ `Test`$$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `Test` BEFORE INSERT ON `categories`
FOR EACH ROW
BEGIN
DECLARE vtype VARCHAR(250) DEFAULT NULL;
SET vtype = NEW.name;
IF (NEW.MDNAME IS NULL)
THEN
-- SET NEW.MDNAME = 'NA';
SET NEW.MDNAME=MD5(NEW.name);
END IF;
END;
$$
DELIMITER ;
This worked for me:
CREATE TRIGGER `update_table_2`
AFTER UPDATE ON `table_1`
FOR EACH ROW
UPDATE table2
JOIN table_1
SET table_2.the_column = NEW.the_column
WHERE table_2.auto_increment_field = OLD.auto_increment_field

how to get value from select query in trigger in mysql5?

How to get value from select query in trigger and insert that value in table?
For an INSERT Trigger query you would use the object NEW
For an UPDATE Trigger query you would use the object OLD and NEW
For a DELETE Trigger query you would use the object OLD
Example 1 : iF you ran INSERT INTO mytable (num) VALUES (10);
In the INSERT trigger, you reference the column as NEW.num (10);
Example 2 : iF you ran UPDATE mytable SET num = 41 WHERE num = 10;
In the UPDATE trigger, you reference OLD.num (10) and NEW.num (41)
Example 3 : iF you ran DELETE mytable num = 104;
In the DELETE trigger, you reference OLD.num (104)
Use something like this:
DELIMITER $$
create trigger my_trigger
AFTER UPDATE on my_update_table
for each row
begin
DECLARE P1,P2 VARCHAR(50);
SELECT PRICENAME INTO P1 FROM PRICEIES WHERE PRICEID=OLD.PRICEID;
SELECT PRICENAME INTO P2 FROM PRICEIES WHERE PRICEID=NEW.PRICEID;
INSERT INTO AUDITLOG(OLDVALUE, NEWVALUE) VALUES (P1,P2);
end $$
DELIMITER ;