N
N
Nikita Sokolov2020-05-19 14:00:48
MySQL
Nikita Sokolov, 2020-05-19 14:00:48

What to fix in my procedure?

I'm trying to create a procedure in phpMyAdmin:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` 
PROCEDURE `GET_EMPLOYEES_WITH_SALARY_MORE_THAN`(IN `target_salary` INT, OUT `counter` INT) 
DETERMINISTIC 
CONTAINS SQL 
SQL SECURITY DEFINER 
BEGIN
DECLARE id integer;
DECLARE name, tel varchar(255);
DECLARE acceptanceDate, dismissalDate DATE;
DECLARE salary integer DEFAULT 0;
DECLARE counter integer DEFAULT 0;
Declare done integer DEFAULT 0;
CREATE TEMPORARY TABLE temp LIKE employees;
DECLARE employeeCursor CURSOR FOR SELECT * FROM employees;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
Open employeeCursor;
WHILE done = 0 DO
  FETCH employeeCursor INTO id, name, tel, acceptanceDate, dismissalDate;
  SET @p0 = id; 
  CALL `GET_SALARY`(@p0, @p1); 
  SELECT @p1 INTO salary;
  if (salary >= target_salary) then
  	INSERT INTO temp SET `idEmployee` = id, `name` = name, `tel` = tel, `acceptanceDate` = acceptanceDate, `dismissalDate` = dismissalDate);
    SET counter = counter + 1; 
  end if;
END WHILE;
Close employeeCursor;
SELECT * FROM temp;
END$$
DELIMITER ;


Gives an error:
#1064 - You have an error in your request. Check the documentation for your version of MariaDB for the correct syntax around 'DECLARE employeeCursor CURSOR FOR SELECT * FROM employees; DECLARE CONTINUE HAN'
on line 14

What's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-05-19
@Rsa97

Before the procedure
DELIMITER $$
At the end of the procedure

END $$
DELIMITER ;

Otherwise, MySQL treats the first semicolon encountered as the end of the CREATE PROCEDURE command.

L
Lazy @BojackHorseman MySQL, 2020-05-19
Tag

CREATE TEMPORARY TABLE temp LIKE employees;
why don't you like it. and even without this line, more errors will come
up : what does the documentation tell us ?
You cannot use CREATE TEMPORY TABLE ... LIKE to create an empty table based on the definition of a table that resides in the mysql tablespace, InnoDB system tablespace (innodb_system), or a general tablespace.
and need to write

CREATE TEMPORARY TABLE `temp` SELECT * FROM `employees` LIMIT 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question