undefined error with MySql insert trigger - mysql

I'm trying to write a MySQL insert trigger:
CREATE TRIGGER `trigger1` BEFORE INSERT ON `COMPANY`
FOR EACH ROW
IF NEW.DOKUMENTTYP LIKE 'test'
AND NEW.STATUS LIKE 'neu'
THEN
INSERT INTO my_tools.testimport( processname, version, step, insstring, initiator )
VALUES ( 'COMPANY_ER', 0, 1, CONCAT( 'DWDOCID=', NEW.DWDOCID ) , 'robot' );
END IF;
But there's an error in my SQL syntax. I cannot find the solution. Could anybody please help me?

Try this
IF (NEW.DOKUMENTTYP LIKE 'test' AND NEW.STATUS LIKE 'neu') ...

Triggers body contains more then one statement, it means that BEGIN..END keywords must be used. And add DELIMITERs, if needed:
DELIMITER $$
CREATE TRIGGER `trigger1`
BEFORE INSERT
ON `Company`
FOR EACH ROW
BEGIN
IF NEW.DOKUMENTTYP LIKE 'test' AND NEW.Status LIKE 'neu' THEN
INSERT INTO my_tools.testimport (processname, VERSION, step, insstring, initiator)
VALUES ('COMPANY_ER', 0, 1, CONCAT('DWDOCID=', NEW.DWDOCID), 'robot');
END IF;
END$$
DELIMITER ;

Related

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

Syntax Error Mysql

When new user registered automatically its permissionID will be 1. It's just that simple, but mysql syntax errors are killing me. Please help
CREATE TRIGGER `user_default_role`
AFTER INSERT ON `users`
FOR EACH ROW
BEGIN
DECLARE #userID int;
SET #userID = (SELECT userID FROM inserted LIMIT 1);
INSERT INTO `user_permission` (`userID`,`permissionID`,`created_at`,`updated_at`) VALUES (#userID,1 ,NOW(),null);
END
This is the 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 '#userID int' at line 5
EDIT: I didn't know about DELIMETER and NEW instead of inserted. Thank you for all who responded so quickly. Here is the updated code:
DELIMITER $$
CREATE TRIGGER `user_default_role` AFTER INSERT ON `users`
FOR EACH ROW
BEGIN
INSERT INTO `user_permission` (`userID`, `permissionID`, `created_at`, `updated_at`)
VALUES (new.userID, 1, NOW(), null);
END;$$
DELIMITER ;
inserted is something from SQL Server, but otherwise your syntax really does suggest MySQL. This may work for you:
DELIMITER $$
CREATE TRIGGER `user_default_role` AFTER INSERT ON `users`
FOR EACH ROW
BEGIN
INSERT INTO `user_permission` (`userID`, `permissionID`, `created_at`, `updated_at`)
VALUES (new.userId, 1, NOW(), null);
END;$$
DELIMITER ;
You are getting syntax error because of following the reasons:
1- Missing Delimiters.
2- Session variable(starting from #) cannot be declared.you can directly set the values to them.
3- There is no inserted table in MySQL where triggers temporary inserts data.You can access inserted values using NEW.
DELIMITER $$
CREATE TRIGGER `user_default_role`
AFTER INSERT ON `users`
FOR EACH ROW
BEGIN
SET #userID =NEW.userID;
INSERT INTO `user_permission` (`userID`,`permissionID`,`created_at`,`updated_at`) VALUES (#userID,1 ,NOW(),null);
END$$
DELIMITER ;

How add condition in trigger?

I have query for crete trigger:
CREATE TRIGGER `Insert` AFTER INSERT ON `HistoryBalance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
But now i woul like get condition:
if (new.Status = 'user'){
CREATE TRIGGER `Insert` AFTER INSERT ON `Balance`
FOR EACH ROW INSERT INTO Winners (name) VALUES (new.ProductID)
}
Tell me please how right crete trigger with my condition ?
You could do something like this:
delimiter $$
CREATE TRIGGER balance_insert AFTER INSERT ON Balance
FOR EACH ROW
BEGIN
if (new.Status = 'user') then
INSERT INTO Winners(name) VALUES (new.ProductID);
end if;
END; $$
delimiter ;
You can read more Here

Mysql Trigger Insert record into another table on specific column update

Alright, I am trying to create a trigger in MySQL that will insert the old date in the table into another table to track the changes that occur. The following tables is what I have. I cannot change the users table but I can change the user_changes table. I created this trigger below but it keeps giving me an error on the line with the insert statement.
users: id, StoreNbr, UserNbr, LvlNbr, Paid1, Paid2
user_changes: id, store_nbr, user_number, level_number, OldPaid1, OldPaid2, create
DROP TRIGGER IF EXISTS usersToChanges;
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END;
You need to use the DELIMTER command to define procedural code:
DROP TRIGGER IF EXISTS usersToChanges;
DELIMITER $$
CREATE TRIGGER `usersToChanges` AFTER UPDATE ON users FOR EACH ROW
BEGIN
IF LENGTH(CONV(NEW.Status, 10, 2)) NOT IN (5,6,8) AND (NOT(NEW.Paid1 <=> OLD.Paid1) OR NOT(NEW.Paid2 <=> OLD.Paid2))
THEN
INSERT INTO user_changes (store_nbr, user_number, level_number, OldPaid1, OldPaid2, created)
VALUES (OLD.StoreNbr, OLD.UserNbr, OLD.LvlNbr, OLD.Paid1, OLD.Paid2, NOW());
END IF;
END $$
DELIMITER ;
The semicolon usually delimits a single command, not procedural code. Notice how I changed the END; to END $$ to finish making the trigger. Then, I switched back to the default delimiter of semicolon.

Trigger syntax error

My first time using triggers. Can anyone please explain why this trigger won't work? The error I'm getting is inconclusive (error near '' at line 5)
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end;
You need to change the delimiter from ; to something else, before defining any stored procedure/functions or triggers.
delimiter ||
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end||
delimiter;