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

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 ;

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

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 creating trigger

I want to create trigger and I have written this query but this does not execute. Please check my query
CREATE
TRIGGER 'blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
I am getting 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 ''blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.del' at line 2
Please run this query
DELIMITER $$
CREATE
TRIGGER blog_after_insert AFTER INSERT
ON blog
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = "DELETE";
ELSE
SET #changetype = "NEW";
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
END$$
DELIMITER ;
Single quotes (') denote string literals - object names, like triggers and tables should use forward quotes, or no quotes at all:
CREATE
TRIGGER blog_after_insert AFTER INSERT -- here
ON blog -- and here
FOR EACH ROW BEGIN
-- rest of code...

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;

SQL trigger giving error

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 ;