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
Related
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 ;
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.
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
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 ;
I have an error in this stored procedure:
Script line: 4 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 'INSERT INTO escuelas_asignacion_maestro (idEscuela,
idCiclo, `idGrado' at line 31
Code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `zz73ff`.`pr_trasladar_asignaciones_por_ciclo` $$
CREATE PROCEDURE `zz73ff`.`pr_trasladar_asignaciones_por_ciclo`
(IN p_ciclo int, IN n_ciclo int)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE escuela, ciclo, grado, nGrupos, maestro INT;
DECLARE fecha datetime;
DECLARE cursor_asignaciones CURSOR FOR
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo
AND idEstatus = 1
ORDER BY idEscuela, idGrado;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SELECT now() INTO fecha;
OPEN cursor_asignaciones;
REPEAT
FETCH cursor_asignaciones INTO escuela, ciclo, grado, nGrupos, maestro;
IF grado > 1 THEN
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro INTO
escuela, ciclo, grado, nGrupos, maestro
FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo AND idEstatus = 1 AND idEscuela = escuela
AND idGrado = (grado - 1);
END IF
INSERT INTO `escuelas_asignacion_maestro`
(`idEscuela`, `idCiclo`, `idGrado`, `numeroGrupos`, `idEstatus`, `idMaestro`, `fechaModificacion`)
VALUES (escuela, n_ciclo, grado, nGrupos, 1,0, fecha);
UNTIL done END REPEAT;
CLOSE cursor_asignaciones;
END $$
DELIMITER ;