MYSQL存储过程 - 无法创建过程

时间:2015-06-24 08:55:41

标签: mysql sql stored-procedures routines

现在尝试以root用户身份创建一个具有一些附加参数的新过程

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 ;

执行上述操作会删除旧过程,但不会创建带有其他参数定义的新过程

通过phpmyadmin运行SP

enter image description here

更新程序

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;

使用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)

如果有人能帮助我理解我在这里做错了什么,我会非常感激。谢谢。

1 个答案:

答案 0 :(得分:1)

语法

的问题很少
  • 您缺少分隔符

  • 程序名称缺少大括号()

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 ;

刚刚在mysql终端测试,它适用于我

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)

对于PHPMyadmin,您不需要代码中的分隔符部分,而是可以从用户界面中选择它们。