I'm using mysqlimport to do mass table inserts (replacing duplicates primary keys). Several tables have date time columns with records containing values '0000-00-00'. What I would like is a trigger which detects these '0000-00-00' values and replaces with '1950-01-01', otherwise keep the datetime value in the record (i.e. when it's not '0000-00-00').
I have tried the following:
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
CASE
WHEN date_col='0000-00-00' THEN SET date_col='1950-01-01';
END CASE
Thank you.
EDIT
Update attempt:
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
IF (NEW.date_col='0000-00-00') THEN
SET NEW.date_col='1950-01-01';
END IF;
Still getting an error in the SET portion.
EDIT 2
WORKING:
DELIMITER $$
CREATE TRIGGER insert_check BEFORE INSERT ON ar
FOR EACH ROW
BEGIN
IF NEW.delivery_date='0000-00-00' THEN
SET NEW.delivery_date='1950-01-01';
END IF;
END; $$
DELIMITER ;
Use IF THEN :
IF (NEW.date_col = '0000-00-00') THEN
SET NEW.date_col='1950-01-01';
END IF;
CREATE TRIGGER set_datetime BEFORE INSERT ON tbl
FOR EACH ROW
IF YEAR(NEW.date_col)=0 THEN
SET NEW.date_col='1950-01-01 00:00:00';
END IF;
Give it a Try !!!
If you want, or need, to use 'case' then this should work:
SET NEW.date_col=CASE WHEN NEW.date_col='0000-00-00' THEN ='1950-01-01'
WHEN NEW.date_col='something else' THEN 'whatever'
ELSE 'default'
END CASE;
Related
I have two tables & need to write a TRIGGER for this scenario.
1. Parameters_Table(int pkid, string param_name, string param_value)
2. Tokens_Table(int pkid,string item,string validity)
I have a requirement of deleting all entries in Tokens_Table if a particular row where param_name='Enterprise' in Parameters_Table changes its 'param_value'. The value is changed from UI. eg., row <7,enterprise,60> changes to <7,enterprise,25> in Parameters_Table then delete all entries from Tokens_Table.
How should I write the trigger. Please guide. (I need to write this for Informix Database, but mysql queries would also help)
DELIMITER $$
CREATE TRIGGER after_update AFTER UPDATE ON Parameters_Table
FOR EACH ROW
BEGIN
IF OLD.param_name ='Enterprise' THEN
IF NEW.param_value != OLD.param_value THEN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END IF;
END IF;
END$$
Just try above code.Hope this will helps.
You can try the following syntax/format:
DELIMITER $$
CREATE TRIGGER delete_tokens AFTER INSERT ON Parameters_Table
FOR EACH ROW
BEGIN
IF NEW.param_name = 'enterprise' THEN
BEGIN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END;
END IF;
END$$
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;
//
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
I have a trigger in MySQL table. Table has about 20 columns.
DELIMITER $$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `completion_date_update`
BEFORE UPDATE ON `orders`
FOR EACH ROW
BEGIN
IF NEW.`order_status` = 'COMPLETED' THEN
SET NEW.`completion_date` = NOW();
END IF;
IF NEW.`order_status` != 'COMPLETED' THEN
SET NEW.`completion_date` = NULL;
END IF;
END$$
What I want is this trigger to update completion_date column only when column order_status is updated. If any other column is updated this trigger should not do anything.
What is actually happening is this trigger is updating completion_date upon any column update.
Can anyone explain to me why does this happen and what did I do wrong?
order_status column is NOT NULL and has possible values as below:
enum('NEW','OPEN','COMPLETED','CANCELLED','REPLACED')
Default is 'NEW'
Thanks
you should also compare if order_status has been changed and that the new value is COMPLETED
IF (NEW.order_status <> OLD.order_status) AND (NEW.order_status = 'COMPLETED') THEN
SET NEW.completion_date = NOW();
END IF;
and you can remove the other IF below.
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 ;