日期:2014-05-16  浏览次数:20910 次

一个有CASE的过程,总是出错,麻烦看看是咋了
SQL code

DROP PROCEDURE IF EXISTS proc_add_personnel;
DELIMITER $$
CREATE PROCEDURE proc_add_personnel(ctt INT)
BEGIN
    DECLARE i INT DEFAULT 0;
    DECLARE j INT;
    DECLARE pos VARCHAR(10);
    SET j = FLOOR(1+(RAND()*6);
    
    CASE j WHEN 1 THEN  pos = '美工';
           WHEN 2 THEN  pos = '网页设计';
           WHEN 3 THEN  pos = '软件工程师';
           WHEN 4 THEN  pos = '架构师';
           WHEN 5 THEN  pos = '行政人员';
           ELSE pos = '其他';
    END CASE
    
    WHILE i<ctt DO
     INSERT INTO personnel(`of_year`,`of_position`,`of_wages`) VALUES (FLOOR(1+(RAND()*10)),'',FLOOR(1000+(RAND()*10000)));
     SET i= i+1;
    END WHILE;
END$$



错误信息:
SQL code

Query : create procedure proc_add_personnel(ctt int) begin  declare i int default 0;  declare j int;  DECLARE pos VARCHAR(10);  set j = ...

Error Code : 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 ';
    
    case j when 1 then  pos = '美工';
           WHEN 2 THEN  pos = '网页设?' at line 6




------解决方案--------------------
DROP PROCEDURE IF EXISTS proc_add_personnel;
DELIMITER $$
CREATE PROCEDURE proc_add_personnel(ctt INT)
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE j INT;
DECLARE pos VARCHAR(10);
SET j = FLOOR(1+(RAND()*6);

CASE j WHEN 1 THEN set pos = '美工' ;
WHEN 2 THEN set pos = '网页设计';
WHEN 3 THEN set pos = '软件工程师';
WHEN 4 THEN set pos = '架构师';
WHEN 5 THEN set pos = '行政人员';
ELSE set pos = '其他';
END CASE;

WHILE i<ctt DO
INSERT INTO personnel(`of_year`,`of_position`,`of_wages`) VALUES (FLOOR(1+(RAND()*10)),'',FLOOR(1000+(RAND()*10000)));
SET i= i+1;
END WHILE;
END$$
DELIMITER ;


------解决方案--------------------
SQL code
DROP PROCEDURE IF EXISTS proc_add_personnel;
DELIMITER $$
CREATE PROCEDURE proc_add_personnel(ctt INT)
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE j INT;
  DECLARE pos VARCHAR(10);
  SET j = FLOOR(1+(RAND()*6));

  CASE j WHEN 1 THEN set pos = '美工' ;
  WHEN 2 THEN set pos = '网页设计';
  WHEN 3 THEN set pos = '软件工程师';
  WHEN 4 THEN set pos = '架构师';
  WHEN 5 THEN set pos = '行政人员';
  ELSE set pos = '其他';
  END CASE;
   
  WHILE i<ctt DO
  INSERT INTO personnel(`of_year`,`of_position`,`of_wages`) VALUES (FLOOR(1+(RAND()*10)),'',FLOOR(1000+(RAND()*10000)));
  SET i= i+1;
  END WHILE;
END$$
DELIMITER ;

------解决方案--------------------
少个分号
------解决方案--------------------
ELIMITER $$
DROP PROCEDURE IF EXISTS proc_add_personnel$$
CREATE PROCEDURE proc_add_personnel(ctt INT)
BEGIN
DECLARE i INT DEFAULT 0;
 DECLARE j INT;
 DECLARE pos VARCHAR(10);
 SET j = FLOOR(1+(RAND()*6));
CASE j WHEN 1 THEN SET pos='美工';
WHEN 2 THEN SET pos = '网页设计';
WHEN 3 THEN SET pos = '软件工程师';
WHEN 4 THEN SET pos = '架构师';
WHEN 5 THEN SET pos = '行政人员';
ELSE SET pos = '其他';
WHILE i<ctt DO
INSERT INTO personnel(`of_year`,`of_position`,`of_wages`) VALUES (FLOOR(1+(RAND()*10)),'',FLOOR(1000+(RAND()*10000)));
SET i= i+1;
END WHILE;

END CASE; 


END$$
DELIMITER ;