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 ;
Related
/* Create trigger for Message system*/
CREATE TRIGGER IF NOT EXISTS TRG_MSG
AFTER INSERT ON Order_Detail
FOR EACH ROW
BEGIN
INSERT INTO Messages (Order_ID, Member_ID, Message, Msg_Date)
VALUES (new.Order_ID, new.Member_ID, 'Your Order is Placed!...', CURRENT_DATE);
END;
In MySQL, I am getting this error...
use DELIMITER
Like
DELIMITER //
CREATE TRIGGER IF NOT EXISTS TRG_MSG
AFTER INSERT ON Order_Detail
FOR EACH ROW
BEGIN
INSERT INTO Messages (Order_ID, Member_ID, Message, Msg_Date)
VALUES (NEW.Order_ID, NEW.Member_ID, 'Your Order is Placed!...', CURRENT_DATE);
END;
DELIMITER ;
MySQL's CREATE TRIGGER syntax does not support the IF NOT EXISTS option.
Also, you need to redefine the DELIMITER before you create the trigger, so the embedded ;s in the body of the trigger do not clash with the surrounding create trigger statement.
DELIMITER //
CREATE TRIGGER RG_MSG
AFTER INSERT ON Order_Detail
FOR EACH ROW
BEGIN
INSERT INTO Messages (Order_ID, Member_ID, Message, Msg_Date)
VALUES (new.Order_ID, new.Member_ID, 'Your Order is Placed!...', CURRENT_DATE);
END//
DELIMITER ;
Demo on DB Fiddle.
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 ;
I'm using WampServer with MySQL Version: 5.7.9 and phpMyAdmmin Version: 4.5.2
I'm trying to write an after insert trigger on Table1 to insert data in Table2 if the data doesn't exist in Table2. Below is my query:
DROP TRIGGER IF EXISTS `insert_customer`;
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;
This gives me the following 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 4
I've tried with delimiter too:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`)
END IF;
END;
|
DELIMITER ;
This gives me the following 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 'END IF;
END' at line 5
What am I doing wrong here? Any help?
Finally I've got it. The correct SQL is:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0 THEN
INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;|
DELIMITER ;
The ; at the end of my INSERT query solved my problem. :D
My guess is the extra delimiter; try changing
END;
|
to
END|
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...
I am trying to fetch some values from php page and perform trigger in mysql database. Which is as below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger` AFTER INSERT OR UPDATE ON `table2`
FOR EACH ROW BEGIN
IF(NEW .flag=0) THEN
INSERT INTO temp(sent,pcount,ncount) VALUES (NEW.sent,NEW.pcount,NEW.ncount);
ELSE
UPDATE temp SET pcount=NEW.pcount AND ncount=NEW.ncount WHERE id = NEW.id;
END IF;
END;
$$
DELIMITER ;
Which gives error:
error Code : 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 'or update ON `table2`
FOR EACH ROW BEGIN
if(NEW .flag=0) then
INSERT INTO' at line 3
I tried to change = with =: but error persist. Any idea whaat the prob?
You can't define a trigger for update and insert at the same time. You have to define 2 triggers:
CREATE TRIGGER `Mysql_insert_Trigger` AFTER INSERT ON `table2` ...
CREATE TRIGGER `Mysql_update_Trigger` AFTER UPDATE ON `table2` ...
I count do it. There was problem in syntax. Here is the correct which perform update insert at same moment in different table and update-inset on same table but with if else:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger2`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger2` AFTER INSERT ON `table2`
FOR EACH ROW BEGIN
IF 'flag'=1 THEN
UPDATE record SET sent=NEW.sent WHERE id=NEW.id;
ELSE
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp VALUES ()
END IF;
END;
$$
DELIMITER ;