Create mysql trigger before insert (Syntax error) - mysql

Hi i am creating some trigger for update the value in insert query before insert the row like this
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
But i received the error like
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 am little confused what's the error in line no 4. Please explain someone. Thanks.

Depending on the client/ide used you may need to set delimiter for example
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
please review https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html

Related

Error Code: 1064. You have an error in your SQL syntax; for the right syntax to use near 'DECLARE #maxdate DATETIME = (SELECT Max [duplicate]

I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;

Declare inside if condition showing syntax error - MYSQL Triggers

I try to update some columns using trigger before insert
DROP TRIGGER IF EXISTS update_p_posts_places;
DELIMITER $$
CREATE TRIGGER update_p_posts_places BEFORE
INSERT
ON
`p_posts` FOR EACH ROW
BEGIN
DECLARE
p_post_group_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
BEGIN
DECLARE
place1_id_ int;
place2_id_ int;
place3_id_ int;
place4_id_ int;
place5_id_ int;
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
END $$
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
DELIMITER ;
It's showing some syntax errors.
#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 'DECLARE
place1_id_ int;
place2_id_ int;
' at line 20
Here is a working trigger. I tested creating it on MySQL 5.7.
CREATE TRIGGER update_p_posts_places BEFORE INSERT ON `p_posts`
FOR EACH ROW
BEGIN
-- all declarations must be before any other statements
DECLARE p_post_group_id_, place1_id_, place2_id_, place3_id_,
place4_id_, place5_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
You can use one DECLARE for multiple local variables, but you must do like var1, var2, var3, ... int. In other words, name the type only once at the end. See documentation: https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
No need for the BEGIN..END inside the IF and definitely do not use $$ until after the last END because that will terminate the parser's interpretation of your whole CREATE TRIGGER statement before it's complete.

MySQL Trigger : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I have problems creating triggers in MySQL 5.1.73
This is de syntax:
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_item`
FOR EACH ROW
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET #alumno := (SELECT user_id from `order` where `order`.id = NEW.order_id)
SET #profesor := (SELECT id_profesor from user where user.id = #alumno)
SET #dto := (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = #profesor)
SET.NEW.descuento = SELECT CAST((((#dto)*(NEW.pvp))/100) AS DECIMAL(10,2))
$$
DELIMITER ;
But there is some 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 'DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET #alu' at line 5
Someone can help me, please?
Thanks u
Note this syntaxs but may not 'work'
drop trigger if exists discount2;
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_stage`
FOR EACH ROW
begin
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
SET profesor = (SELECT id_profesor from user where user.id = alumno);
SET dto = (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = profesor);
SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
end $$
DELIMITER ;
The syntax to create a BEFORE INSERT Trigger in MySQL is:
CREATE TRIGGER trigger_name
BEFORE INSERT
ON table_name FOR EACH ROW
BEGIN
-- variable declarations
-- trigger code
END;
If you want to execute multiple statements in your trigger, you should use BEGIN and END.
The Reason for using DELIMITER $$ before and after the trigger declaration is to change the delimiter from ; (default) to $$.
If you didn't change the DELIMITER, the first semicolon in your trigger code may be interpreted as the end of trigger declaration. Thus the code following may result in syntax errors.
DELIMITER $$
CREATE TRIGGER `discount2`
BEFORE
INSERT ON `order_stage`
FOR EACH ROW
BEGIN
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
SET profesor = (SELECT id_profesor from user where user.id = alumno);
SET dto = (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = profesor);
SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
END;
$$
DELIMITER ;
Read More Here

SQL: Create table with term names (String containing years)

I would like to create a table (two columns, first is auto incremental) which contains the term names, e.g. "SS 2000" and "WS 2000/2001" (for summer and winter term).
I tried the following:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
SET #YearEnd = 2014;
Declare #YearFrom integer = #YearEnd - 100;
Declare #Term varchar = '';
while #YearFrom < #YearEnd Begin
#Term = concat('SS ', #YearFrom);
Insert into terms (term) VALUES #Term;
Set #YearFrom = #YearFrom + 1;
End
END
but I already get an error in line 3: SQL query:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
MySQL said: Documentation #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 '#YearEnd integer' at line 3
After reading the comments from Abishek and Stuart, I tried the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END;
DELIMITER ;
This results in just the DROP PROCEDURE command being successfully processed. Even when removing this line and changing the first lines to:
DELIMITER $$
CREATE PROCEDURE create_terms()$$
BEGIN
it doesn't work, the SQl console just writes "ERROR" and that's it.... :(
There are quite a bunch of errors here
You should use procedure variables
The Syntax for insert is INSERT INTO Table(columns) VALUES(values);
You probably also want to insert the term, not the year end
Your syntax for while is wrong
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END
Fiddle here
All variable staring # are user variables and need not to be declared, procedures has their local variables not prefix with #, try like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
Declare YearEnd integer;
SET YearEnd = 2014;
Declare YearFrom integer = YearEnd - 100;
Declare Term varchar = '';
while YearFrom < YearEnd Begin
Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES YearFrom;
Set YearFrom = YearFrom + 1;
End;
END;
DELIMITER ;
Here is help

Declare variable syntax invalid in MySQL Workbench?

I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;