MSQL Error 1193--updating on the same table - mysql

I'm trying to create a trigger to update a column in the same table after another update. I am given a SQL Error (1193): Unknown system variable
I thought this is fairly straightforward, but it doesn't seem to like it very much. Please help!
CREATE TRIGGER `redeem_gc`
BEFORE UPDATE ON `giftcards`
FOR EACH ROW
IF NEW.value = 0 THEN
SET `status`= '2'
END IF
what the code should do is if the value reaches 0, then update the status to 'x'
Thanks.

delimiter |
CREATE TRIGGER `redeem_gc` BEFORE UPDATE ON `giftcards`
FOR EACH ROW BEGIN
IF NEW.value = 0 THEN
SET NEW.`status`= '2';
END IF;
END
|
delimiter ;

Related

MySQL Trigger Not Compiling and logic

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?

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

Trigger firing on every update instead of just one column

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.

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

MYSQL Trigger set datetime value using case statement

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;