Why does the trigger have no output and is not created?
create trigger chronik BEFORE UPDATE
ON eastatus_t
FOR EACH ROW
BEGIN
insert into sendechronik_t(mandant, fk_eakopf_posnr, lfd_nr, statusdatum, fk_statusart_mc, statuszeit, fk_ort_status)
SELECT (mandant, fk_eakopf_posnr, lfd_nur, statusdatum, fk_statusart_mc, statuszeit, fk_ort_status)
FROM eastatus_t
END$$
DELIMITER ;
Related
Trying to create a Delete Trigger. If the sku is found in sold table need to delete it in not sold table
delimiter $$
drop trigger delete_sold_trigger $$
create trigger delete_sold_trigger after insert on soldtable
for each row
begin
IF soldtable.sku EXIST IN notsoldtable THEN
DELETE from notsoldtable WHERE notsoldtable.sku=soldtable.sku;
END IF;
end$$
delimiter ;
CREATE TRIGGER delete_from_notsoldtable_trigger
AFTER INSERT ON soldtable
FOR EACH ROW
DELETE FROM notsoldtable
WHERE notsoldtable.sku=NEW.sku;
I have to do two triggers, first one, after insert in test1 table, to insert in test2 table another row. All right, the problem is that i have to do same trigger in reverse form, after insert row in test2 table, insert new row on test1. I know that trigger can't insert in a table when a trigger is firing, but i need resolve this. I don't have a lot experience with triggers, i hope that this could be resolved.
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END$$
DELIMITER ;
Bidirectional trigger:
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test2`
FOR EACH ROW
BEGIN INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END$$
DELIMITER ;
Thanks.
Edit:
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
The infinite loop can't be resolved. Triggers can't be bidirectional... I think that i must be use a PROCEDURE...
There is a posible solution for the problem:
DELIMITER $$ CREATE TRIGGER `ai_test1_test2` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest2 FROM test2 WHERE vtest2 = new.vtest1 ) THEN
INSERT INTO test2 (vtest2) VALUES (new.vtest1)
END IF;
END$$
DELIMITER ;
DELIMITER $$ CREATE TRIGGER `ai_test2_test1` AFTER INSERT ON `test1`
FOR EACH ROW
BEGIN
IF NOT EXISTS( SELECT vtest1 FROM test1 WHERE vtest1 = new.vtest2 ) THEN
INSERT INTO test1 (vtest1) VALUES (new.vtest2)
END IF;
END$$
DELIMITER ;
After insert in the table Aluguel update field Status to busy.
Not work.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = busy
WHERE id_apart = apartamento_id_apart;
END$$
DELIMITER ;
you have to use new key word
DELIMITER $$ CREATE TRIGGER Tgr_Status_Update AFTER UPDATE
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
---------------------------------------insert trigger -----------------
DELIMITER $$ CREATE TRIGGER Tgr_Status_Insert AFTER INSERT
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
To refer to a column from the table you're inserting into, you need to use NEW.column_name.
And if busy is a string, you need to put it in quotes.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = 'busy'
WHERE id_apart = NEW.apartamento_id_apart;
END$$
DELIMITER ;
DEMO
I want a trigger that deletes the row in user_briefcases after a row is inserted in user_briefcases_sold
both tables have briefcase_id and user_id
I have no idea why this wont work:
CREATE TRIGGER delete_user_briefcase_when_sold
DELIMITER $$
AFTER INSERT ON user_briefcases_sold FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id && user_id = NEW.user_id;
END;
$$ DELIMITER ;
You have syntax error
try this
DELIMITER $$
CREATE
TRIGGER delete_user_briefcase_when_sold AFTER INSERT
ON user_briefcases_sold
FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id AND user_id = NEW.user_id;
END$$
DELIMITER ;
My trigger will not work.
Basically when a user_briefcase is created i want to also create a row in user_complementary_info if it not already exist, where user_complementay.user_id = the newly created user_briefcase.user_id
My trigger
DROP TRIGGER IF EXISTS complementary_user_info
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) user_complementary WHERE user_id=NEW.user_briefcases.user_id) = 0 THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id)
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS complementary_user_info;
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM user_complementary_info WHERE user_id = NEW.user_id) THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id);
END IF;
END $$
DELIMITER ;