I want to update each row in MySQL table using stored procedure but the problem is my code below is not working:
DELIMITER //
CREATE PROCEDURE UPDATESCORE()
BEGIN
LOOP
UPDATE scoretable SET `final_average` = (`term1_result` + `term2_result`) / 2;
END LOOP
END
DELIMITER ;
Just Remove the Loop From your procedure as Update work for all row if you don't have Where clause. Also Update query work by default in safe update mode so you need to set that to false.
SET SQL_SAFE_UPDATES = 0;
and then there is no error when you run that procedure also.
Your Update query would:
DELIMITER //
CREATE PROCEDURE UPDATESCORE()
BEGIN
UPDATE scoretable SET `final_average` = (`term1_result` + `term2_result`);
END //
DELIMITER ;
Remove loop, update works on each row if there is no where clause.
Related
I am trying to implement the following trigger but I am getting a MySQL syntax error. The idea is that after each time the table is updated I want to set the updated 'valid' field to 0 if the 'banned' field has been changed to 1. Also is the logic below correct?
Create Trigger `customers` Before Update on `customers` for each row BEGIN
set new.valid = 0 WHERE new.banned = 1;
END;
Yes the trigger should be something as
delimiter //
Create Trigger `customers_update`
Before Update on `customers`
for each row
BEGIN
IF new.banned = 1
THEN set new.valid = 0 ;
END IF ;
END;//
As you can see I have used delimiter in the trigger which is needed for the trigger. There is a nice explanation of this here What does DELIMITER // do in a Trigger?
I am attempting to create a trigger to update quantities between 2 separate databases. This query runs successfully, but when I show triggers in mysql, it brings up an empty set. Any help would be much appreciated.
delimiter $$ CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code
END $$
delimiter ;
That's because your code has two synax errors:
delimiter $$ --delimiter statements need to be on separate lines
CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN
UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code; -- ; was needed here
END $$
delimiter ;
I hav a table with several fields. One field is "date_assigned" and the other is "assigned". "date_assigned" has a datatype of timestamp and can be null. "assigned" has a datatype of tinyint and the values are either 0 (default; 'not assigned') or 1 ('assigned').
I would like to create a trigger that would automatically update the "assigned" value to 1 when "date_assigned" is updated with a value (is not null).
I've used triggers before, but have not used them in conjunction with checking if a value is null. I'm unclear on the syntax, so any help would be appreciated. So far, I've tried:
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;
I just get Error Code: 1064. I looked upo the code, and it appears that it's a syntax error. So what syntax mistake am I making, and is this even the correct 'grammar'?
Try putting BEGIN in a new line as follows.
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW
BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END; //Change here also.
$$
DELIMITER ;
This is because of default delimiter in MySQL set to ;. So, the first line should look like DELIMITER $$;
DELIMITER $$;
CREATE TRIGGER `<database>`.`<trigger_name>`
AFTER UPDATE ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;
Currently, I have bunch of triggers that do the same thing. This is copy-pasted for every table that needs this functionality.
delimiter $$
create trigger user_before_insert before insert on user for each row
begin
set NEW.create_time = CURRENT_TIMESTAMP;
set NEW.create_user_id = #user_id;
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
create trigger user_before_update before update on user for each row
begin
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
Is it possible to wrap the lines that modify OLD and/or NEW into stored procedures that are called via triggers? Something like this:
delimiter $$
create procedure autofill_on_insert(inout NEW data_type) -- what would be the data_type?
begin
set NEW.create_time = CURRENT_TIMESTAMP;
set NEW.create_user_id = #user_id;
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
create procedure autofill_on_update(inout NEW data_type) -- what would be the data_type?
begin
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
delimiter ;
create trigger user_before_insert before insert on user
for each row call autofill_on_insert(NEW);
create trigger user_before_update before update on user
for each row call autofill_on_update(NEW);
Additional question: if this is possible, is there any way to check if NEW contains specific columns? There are tables that do not have modify_time and modify_user_id.
I can say that NEW cannot be passed into the procedure, because NEW is an alias that represents a row. Procedure's arguments have to be scalar values, like INT, VARCHAR, etc..
About the 'SET NEW.create_time = CURRENT_TIMESTAMP;'; if field create_time is a TIMESTAMP, you could set CURRENT_TIMESTAMP as default value for it.
Is it possible to declare that a column in MySQL should be unmodifiable once initially set? I have a column that has the following definition: created timestamp default current_timestamp and I'd like to make sure that nobody messes with it after my rows are created.
You can accomplish this using a BEFORE UPDATE trigger:
DELIMITER $$
CREATE TRIGGER trFoo BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
IF NEW.Bar != OLD.Bar THEN
SET NEW.Bar = OLD.Bar;
END IF;
END$$
DELIMITER ;