MYSQL Stored Procedure - Can not create procedure

2.2k views Asked by At

Now trying to create a new procedure as root user with some additional parameters

DELIMITER //
USE `user_main_table`;
DROP PROCEDURE IF EXISTS insert_user_temp;
CREATE DEFINER='root'@'localhost' PROCEDURE insert_user_temp()
 BEGIN 
  DECLARE row_coun_no INT;
  SELECT COUNT(`id`) INTO row_coun_no FROM user_temp;
  IF row_coun_no != 0 THEN
    TRUNCATE `user_data`;
    INSERT INTO user_data (employee_id,employee_code,employment_type,`emp_name`,first_name,middle_name,last_name,photo_file_name,email_id)
    SELECT employee_id,`employee_code`,`employment_type`,CONCAT(`first_name`," ",`last_name`),`first_name`,`middle_name`,`last_name`, `email_id`, `photo_file_name` FROM `user_temp`;
    TRUNCATE user_temp;
    UPDATE  `demo_user`.`tbl_users`, `user_main_table`.`user_data` SET tbl_users.profile_pic = user_data.photo_file_name
  END IF;
END;

DELIMITER ;

Executing above deletes the old procedure but does not create new with additional params defined

Running SP through phpmyadmin

enter image description here

UPDATED PROCEDURE

USE `user_main_table`;
DROP PROCEDURE IF EXISTS insert_user_temp;
CREATE DEFINER='root'@'localhost' PROCEDURE insert_user_temp()
 BEGIN 
  DECLARE row_coun_no INT;
  SELECT COUNT(`id`) INTO row_coun_no FROM user_temp;
  IF row_coun_no != 0 THEN
    TRUNCATE `user_data`;
    INSERT INTO user_data (employee_id,employee_code,employment_type,`emp_name`,first_name,middle_name,last_name,photo_file_name,email_id)
    SELECT employee_id,`employee_code`,`employment_type`,CONCAT(`first_name`," ",`last_name`),`first_name`,`middle_name`,`last_name`, `email_id`, `photo_file_name` FROM `user_temp`;
    TRUNCATE user_temp;
    UPDATE  `demo_user`.`tbl_users`, `user_main_table`.`user_data` SET tbl_users.profile_pic = user_data.photo_file_name
  END IF;
END;

Executing Procedure using mysql termial

CREATE PROCEDURE insert_user_temp_to_user()
BEGIN 
DECLARE row_coun_no INT;
SELECT COUNT(`id`) INTO row_coun_no FROM user_temp;
IF row_coun_no != 0 THEN
TRUNCATE `user_hrms`;
INSERT INTO user_hrms (employee_id,employee_code,employment_type,`emp_name`,first_name,middle_name,last_name,reporting_authority,department,designation,gender,location_name,branch_id,mobile_number,extension_number,email_id,`status`,`birthdate`,`anniversary`,`photo_file_name`,mtd,ytd,experience ,primary_skills,secondary_skills,joining_date,bu,award_category,award_category_desc)
SELECT employee_id,`employee_code`,`employment_type`,CONCAT(`first_name`," ",`last_name`),`first_name`,`middle_name`,`last_name`,`reporting_authority`,`department`,`designation`,`gender`,`location_name`,`branch_id`,`mobile_number`,`extension_number`,`email_id`,`status`,`birthdate`,`anniversary`,`photo_file_name`,mtd,ytd,experience,primary_skills,secondary_skills,joining_date,bu,award_category,award_category_desc FROM `user_temp`; 
TRUNCATE user_temp;
UPDATE `demo_intranet`.`tbl_users`, `intranet`.`user_hrms` SET tbl_users.profile_pic = user_hrms.photo_file_name
WHERE tbl_users.email = user_hrms.email_id;
END IF;
END;//
Query OK, 0 rows affected (0.02 sec)

If someone could help me understand what I am doing incorrectly here, I would greatly appreciate it. Thank you.

1

There are 1 answers

18
Abhik Chakraborty On

Few issues with the syntax

  • You are missing the delimiter

  • The procedure name is missing braces ()


delimiter //
CREATE PROCEDURE insert_user_temp()
 BEGIN 
  DECLARE row_coun_no INT;
  SELECT COUNT(`id`) INTO row_coun_no FROM user_temp;
  IF row_coun_no != 0 THEN
    TRUNCATE `user_data`;
    INSERT INTO user_data (employee_id,employee_code,employment_type,`emp_name`,first_name,middle_name,last_name,photo_file_name,email_id)
    SELECT employee_id,`employee_code`,`employment_type`,CONCAT(`first_name`," ",`last_name`),`first_name`,`middle_name`,`last_name`, `email_id`, `photo_file_name` FROM `user_temp`;
    TRUNCATE user_temp;
    UPDATE  `demo_user`.`tbl_users`, `main_user`.`user_data` SET tbl_users.profile_pic = user_data.photo_file_name
    WHERE  tbl_users.email = user_data.email_id;
  END IF;
END;//

delimiter ;

Just tested in mysql terminal and it works for me

mysql> use test ;
mysql> delimiter //
mysql> CREATE PROCEDURE insert_user_temp()
    ->  BEGIN 
    ->   DECLARE row_coun_no INT;
    ->   SELECT COUNT(`id`) INTO row_coun_no FROM user_temp;
    ->   IF row_coun_no != 0 THEN
    ->     TRUNCATE `user_data`;
    ->     INSERT INTO user_data (employee_id,employee_code,employment_type,`emp_name`,first_name,middle_name,last_name,photo_file_name,email_id)
    ->     SELECT employee_id,`employee_code`,`employment_type`,CONCAT(`first_name`," ",`last_name`),`first_name`,`middle_name`,`last_name`, `email_id`, `photo_file_name` FROM `user_temp`;
    ->     TRUNCATE user_temp;
    ->     UPDATE  `demo_user`.`tbl_users`, `main_user`.`user_data` SET tbl_users.profile_pic = user_data.photo_file_name
    ->     WHERE  tbl_users.email = user_data.email_id;
    ->   END IF;
    -> END;//
Query OK, 0 rows affected (0.00 sec)

For PHPMyadmin you do not need the delimiter section in the code rather you can select them from the user interface.