I'm trying to remove a row after inserted in a temp table and I get this error:
INSERT INTO `temp_program_counter` (`id`, `program_id`) values (NULL, '275')
Can't update table 'temp_program_counter' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
And here is my trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` AFTER INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
this table is a temp table that get's data with delay and after receiving must update the main table and be deleted from temp.
thanks for helping in advance.
You probably have already resolved this in some way but I'll post the answer anyway:
You cannot refer to the same table that triggered the trigger in a trigger (or a combination of trigger and stored procedure). This could mean an infinite loop, so sql prevents it by giving that error, resulting in always the same error.
Try BEFORE INSERT
Because, you are trying to delete a record which has just been inserted but may be not yet committed in this session. Hence you are getting the error.
If you use BEFORE INSERT, then all other record in the table with id=new.id would be deleted and the new record will be inserted successfully.
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` BEFORE INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.
I have created table in Slq yog which was working fine. then i created same on mysql terminal.
this also executes with no error:
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$ DELIMITER ;
But after this when I enter show triggers or any other command it ask for -> no input
so I need to terminate mysql. Then when I check show triggers says Empty set.
Where is the mistake here?
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$
DELIMITER ;
new delimiter must be on another line
My trigger fails when i try to insert a new row in my table because Mysql doesn't support updating rows in the same table the trigger is assigned to. Does anyone have suggestions on a good workaround/alternative?
My trigger:
-- Trigger DDL Statements
DELIMITER $$
CREATE TRIGGER check_sequence
BEFORE INSERT ON data FOR EACH ROW
BEGIN
IF EXISTS(SELECT TRUE FROM data WHERE sequence = NEW.sequence) THEN
UPDATE data SET sequence=sequence+1 WHERE sequence >= NEW.sequence;
END IF;
END $$
DELIMITER ;
Error Sql that is displayed when i try to insert new row:
ERROR 1442 (HY000): Can't update table 'data' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for answer,
In MySQL, You can't update (or insert in) table A in a trigger for table A.
http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html
You can try a sproc and use a transaction, or consider PostgreSQL, which can do this.
I have two tables called account and transaction. I need to insert data to transaction automatically when account inserts data. I created this trigger in MySQL. It gives error 1064 (Syntax error). What is the problem?
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
MySQL gets confused with semicolons inside the BEGIN END block, so you must use DELIMITER to temporarily force MySQL to use a different delimiter.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;
You probably need to replace:
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
with:
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
Here is you original trigger
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
Replace account. with NEW. (optional : replace " with ')
DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
NEW.deposit,NEW.accNo,
'Teller','Cash','Deposit');
END $$
DELIMITER ;
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
-- trigger body
-- this code is applied to every
-- inserted/updated/deleted row
END;
Seems like you're missing a table name after 'account'. (edit: or a database name before account, I'm not certain as to what account pertains to)