SQL trigger giving error - mysql

I'm trying to call a stored procedure to create multiple rows using some of the cell values of the new row inserted in the table resource but I'm getting an error. Here is my query:
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END;
#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 3
What am I doing wrong?

delimiter |
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END
|
delimiter ;
If you don't set another delimiter than ;, then the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the statement should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;

Related

create trigger not working in mysql

i am trying to write a trigger which validates insert statements before inserting in the table
table structure:
This is the code that i wrote
DELIMITER
$$
CREATE TRIGGER check_date_format
INSERT BEFORE ON
job_histry FOR EACH ROW
BEGIN
SET #bool=NEW.end_date LIKE '--/--/----';
IF #bool THEN
INSERT
INTO
job_histry
VALUES(
NULL,
NEW.start_date,
NEW.end_date,
NEW.job_id,
NEW.department_id
) ;
END IF ;
END
$$
This is the error that i received:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT BEFORE ON job_histry FOR EACH ROW BEGIN SET #bool=NEW.end_date LIKE '' at line 2
why is this happening?
I am using localhost xampp server.
You have the trigger type (INSERT) and time (BEFORE) in the wrong order.
It should read
BEFORE INSERT ON job_histry

How to insert a value from a in another table via trigger

I want to create a trigger to insert a value in another table when a value is inserted in the first table.
So far my trigger looks like this:
CREATE TRIGGER tgIdPass
AFTER INSERT
ON tbuser FOR EACH ROW
BEGIN
DECLARE vIdPass INT
SET vIdPass = NEW.id
INSERT INTO tbpass.fkUser VALUES vIdPass
END
When I try to run the code, it gives this error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET vIdPass = NEW.id INSERT INTO tbpass.fkUser VALUES vIdPass END' at line 8"
So anyone can illuminate my on why I'm getting this error?
Need DELIMITERs and statement terminators.
DELIMITER //
CREATE TRIGGER tgIdPass
AFTER INSERT
ON tbuser FOR EACH ROW
BEGIN
DECLARE vIdPass INT; -- terminate statements
...
END
//
DELIMITER ;

Error 1064 on Mysql trigger

I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;
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 6
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;

MySQL before insert trigger filling omitted column

I am trying to make make a trigger, that will fill column B with value from column A if column B was not explicitly set in insert query. (column B is set to allow NULL and to default to NULL value)
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN SET NEW.valueB = NEW.valueA ;
END
$$
But I am getting this error (not very helpful).
ERROR 1064 (42000): 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 4
When I tried to locate this problematic empty string '' making the query like one word per line, mysql marked the line with '=' character as problematic.
I double checked the query for any non-ascii characters.
I am using mysql version 5.5.37-0ubuntu0.12.10.1 through commandline (eg not phpmyadmin).
You need to close the IF with END IF
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.valueB IS NULL THEN
SET NEW.valueB = NEW.valueA ;
END IF ;
END;$$
delimiter ;
Check the example here http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
You forgot to close the IF statement. Read the syntax here. I think the code below will work for you.
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN
SET NEW.valueB = NEW.valueA ;
END IF
END $$
DELIMITER

SQL syntax error in mysql when creating trigger

I have a problem when creating a trigger in mysql i get this 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 7
This is my first time creating triggers for mysql help
Here is the code:
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;
You need to set the delimiter
delimiter //
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;//
delimiter ;
CREATE TRIGGER `insertAfterUser ` BEFORE INSERT ON `members` FOR EACH ROW BEGIN INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES('12','0.0'); END;