I am new to MySQL trigger. I want to make a trigger that before the update is made from my emp_audit table will check first if the empno from my employees table already exists. If it exists then do the update otherwise do nothing.
I encounter the following error:
Error 1064
The code is:
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW();
END IF;
END$$
DELIMITER;
The UPDATE syntax does not include the INTO keyword.
Please check if the below updated query works.
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;
I think, you can even remove the empno from you SET(update) list and try the below query,
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;
Related
i am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;
So I have the following query for a trigger:
DELIMITER $$
CREATE TRIGGER user_log_update BEFORE UPDATE on user_log
FOR EACH ROW
BEGIN
insert into user_log
(id, user_id, name, username, password, email, user_type_id, created)
VALUES(OLD.id, OLD.user_id, OLD.name, OLD.username, OLD.password, OLD.email, OLD.user_type_id, OLD.created);
IF (OLD.id = 1) THEN
SET OLD.id = OLD.id +1;
END IF;
SELECT * FROM user_log;
END$$
DELIMITER ;
When I try to execute this part of the script, I get Error Code: 1362. Updating of OLD row is not allowed in trigger
I don't know why I got this error and I don't see anything wrong in the syntax.
Does anyone know how to fix it?
So, increment new instead:
DELIMITER $$
CREATE TRIGGER user_log_update BEFORE UPDATE on user_log
FOR EACH ROW
BEGIN
insert into user_log(id, user_id, name, username, password, email, user_type_id, created)
VALUES(OLD.id, OLD.user_id, OLD.name, OLD.username, OLD.password, OLD.email, OLD.user_type_id, OLD.created);
IF (OLD.id = 1) THEN
SET NEW.id = OLD.id +1;
END IF;
END$$
DELIMITER ;
delimiter |
create trigger display_student_table
before insert on student_table
for each row
Begin
set new.student_name=trim(new.student_name);
set new.branch=trim(new.branch);
set new.address=trim(address);
End ;
|
delimiter ;
DELIMITER $$
CREATE PROCEDURE update_salary()
BEGIN
UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
End $$
DELIMITER ;
I create stored procedure to My_sql to update tables values. It is working but, I want to change this to update only one of set of rows.
e.g. When I give 'Manager' update only that column.
How can i do this?
you can do this with an IF statement inside your stored procedure like so..
DELIMITER $$
CREATE PROCEDURE update_salary(IN employeeType VARCHAR(255))
BEGIN
IF (employeeType = "Seinor Manager") THEN
UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
ELSEIF (employeeType = "Manager") THEN
UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
ELSE
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END IF;
End $$
DELIMITER ;
or you could do it with a case statement too if that suites you better
DELIMITER $$
CREATE PROCEDURE update_salary(IN employeeType VARCHAR(255))
BEGIN
CASE employeeType
WHEN "Seinor Manager")
THEN UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
WHEN "Manager"
THEN UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
ELSE
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END;
End $$
DELIMITER ;
I am trying to create a mysql trigger. The error code 1064 is very generic. The trigger is to see if a record exists for a particular key; and if it does update a particular column in the newly inserted row with that value. I am using a select query on the same table as the trigger is on. Does that cause a problem? Here is the code:
DELIMITER $$
CREATE TRIGGER classid_insert
AFTER INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
NEW.class_id = UUID()
ELSE
NEW.class_id = #class_id
END IF;
END
DELIMITER ;
your entire trigger code should look like below. remove the spaces after DELIMITER $$ and before DELIMITER ;. Also, the last END need to changed to END $$. Also, it has to be an BEFORE trigger and not an AFTER trigger.
Moreover, NEW.class_id = UUID() is not correct.It's missing SET as
SET NEW.class_id = UUID();
CORRECTED TRIGGER CODE:
DELIMITER $$
CREATE TRIGGER classid_insert
BEFORE INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
SET NEW.class_id = UUID();
ELSE
SET NEW.class_id = #class_id;
END IF;
END $$
DELIMITER ;
I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.