mysql update trigger old, new row's column log - mysql

my update trigger
BEGIN
DECLARE s VARCHAR(100);
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END
inserted row oldValue column always null insert, whats my problem ?
I have delete trigger is succes work
my Delete trigger:
BEGIN
DECLARE s VARCHAR(60);
SET s = CONCAT('&authUId=', OLD.authUId,'&autPId=', OLD.autPId,'&authTypeId=', OLD.authTypeId,'&authValue=', OLD.authValue);
INSERT INTO l_dblog(src,newId,oldValue)
VALUE('auth_del',OLD.authId,s);
END
Thank you...

Set default value of variable s as blank in your update query and then check weather trigger is working or not. If not than what error you got after running the trigger.
BEGIN
DECLARE s VARCHAR(100) DEFAULT '';
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END

Related

How to get the value of a column if its name is stored in a variable inside the trigger?

When updating data in database tables, I need to write to a separate table: table name, change date, old column value, new column value.
I wrote a trigger:
CREATE TRIGGER `user_update_trigger`
AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
DECLARE done int default false;
DECLARE col_name CHAR(255);
DECLARE counter INTEGER(11);
DECLARE column_cursor cursor for SELECT `column_name`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='test'
AND `TABLE_NAME`='users';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open column_cursor;
myloop: loop
fetch column_cursor into col_name;
if done then
leave myloop;
end if;
/*SET #old_val = OLD.{{col_name}}; <------ HERE */
/*SET #new_val = NEW.{{col_name}};<------ HERE */
if #old_val <> #new_val then
/*INSERT INTO `logs` ....*/
end if;
end loop;
close column_cursor;
END;
I need to get the value from OLD and NEW.
But column name is in the variable col_name.
I tried:
SET #old_q = CONCAT('OLD.', col_name);
SET #new_q = CONCAT('NEW.', col_name);
PREPARE old_prepare_query FROM #old_q;
PREPARE new_prepare_query FROM #new_q;
EXECUTE old_prepare_query USING #old_val;
EXECUTE new_prepare_query USING #new_val;
But I got an error:
Error Code: 1336. Dynamic SQL is not allowed in stored function or trigger

Update one mysql column when another is edited

I'm trying to make a datetime field that automatically gets updated with the current time only if there was a change to a certain field.
It seems I have a syntax error.
CREATE OR ALTER TRIGGER last_progress_date
ON wp_task_mgr
AFTER UPDATE
AS BEGIN
IF UPDATE (progress_percentage)
SET last_progress_date = GETDATE()
END
Just for future reference, I found the answer here:
https://dba.stackexchange.com/questions/125203/simple-trigger-to-update-two-columns-in-one-table
MySQL query:
DELIMITER //
CREATE TRIGGER trig_1 before insert
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date is not null and new.end_date='' then
set new.end_date=new.due_date;
end if;
IF new.end_date is not null and new.due_date='' then
set new.due_date=new.end_date;
end if;
END;
//
DELIMITER //
CREATE TRIGGER trig_2 before update
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date <>old.due_date then
set new.end_date=new.due_date;
end if;
IF new.end_date <> old.end_date then
set new.due_date=new.end_date;
end if;
END;
//

MySQL ON UPDATE trigger - update the same table with CURRENT_TIMESTAMP and condition

I have a MySQL(5.1.67) table 'tracker' with TIMESTAMP field 'close_time'.
In the same table I have string field 'status'.
I want to update close_time field with CURRENT_TIMESTAMP value when status field updated to specific text value, for example 'Closed'.
Here is sample with INSERT:
How can I write a trigger that updates rows in the same table, before the insert is commited?
I tried something like that:
CREATE TRIGGER `close_time_trigger` BEFORE UPDATE ON `tracker` FOR EACH ROW
BEGIN
IF NEW.status = 'Closed' THEN
SET NEW.close_time = CURRENT_TIMESTAMP;
END IF;
END
But I got error: You have an error in your SQL syntax ... at line 4.
I tried some other variations which I found but none of them works.
Please help to create correct trigger for my case.
Did you set the DELIMITER?
This should work:
DELIMITER $$
CREATE TRIGGER `close_time_trigger` BEFORE UPDATE ON `tracker` FOR EACH ROW
BEGIN
IF NEW.status = 'Closed' THEN
SET NEW.close_time = CURRENT_TIMESTAMP;
END IF;
END $$
DELIMITER ;
you didn't tell it what to update.
CREATE TRIGGER `close_time_trigger` BEFORE UPDATE ON `tracker` FOR EACH ROW
BEGIN
IF NEW.status = 'Closed' THEN
update tracker
SET NEW.close_time = CURRENT_TIMESTAMP;
END IF;
END

error in trigger creation also how to optimise it

Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."

MySQL trigger if condition exists

I'm trying to write an update trigger that will only update a password when a new password is set in the update statement but I'm having a terrible time trying to nail down the syntax. This should be a no-brainer but I'm just not finding the solution.
Here's my code:
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password <> '') THEN
SET NEW.password = PASSWORD(NEW.password);
END IF;
END;
I've tried:
IF (NEW.password <> NULL) THEN
IF (NEW.password) THEN
IF NEW.password <> NULL THEN
IF (NEW.password > 0) THEN
IF (NEW.password != NULL) THEN
And I'm sure many other combinations but it's just not working. Does anyone have any insight?
I think you mean to update it back to the OLD password, when the NEW one is not supplied.
DROP TRIGGER IF EXISTS upd_user;
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '') THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
However, this means a user can never blank out a password.
If the password field (already encrypted) is being sent back in the update to mySQL, then it will not be null or blank, and MySQL will attempt to redo the Password() function on it. To detect this, use this code instead
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '' OR NEW.password = OLD.password) THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
Try to do...
DELIMITER $$
CREATE TRIGGER aumentarsalario
BEFORE INSERT
ON empregados
FOR EACH ROW
BEGIN
if (NEW.SALARIO < 900) THEN
set NEW.SALARIO = NEW.SALARIO + (NEW.SALARIO * 0.1);
END IF;
END $$
DELIMITER ;