如何在PROCEDURE MySQL的CONCAT()中使用UPDATE

时间:2017-03-16 18:19:17

标签: mysql stored-procedures

我尝试通过PROCEDURE更新存在的表。但是我收到一个错误:MySQL:#1064 - 你的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'= iOS,phone_manufacture = Apple,phone_model = iPhone,os_vers = iOS XX'第1行附近使用正确的语法/ p>

我的程序:

BEGIN 
DECLARE a, b INT;
DECLARE a1, a2, a3, a4 TEXT;
DECLARE cur1 CURSOR FOR SELECT id FROM z7_group;  
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;  
OPEN cur1;   
SET b = 0; 
SET a1 = "iOS";
SET a2 = "Apple";
SET a3 = "iPhone";
SET a4 = "iOS X.X";
WHILE b = 0 DO  
FETCH cur1 INTO a;
IF b = 0 THEN
 SET @data_ all_group = (SELECT `myTables` FROM `all_group` WHERE id= a);
 SET @s = CONCAT('UPDATE ', @data_ all_group, 'SET `os` = ',a1,', `phone_manufacture` = ',a2,', `phone_model` = ',a3', `os_vers` = ',a4,');
 PREPARE stmt3 FROM @s;
 EXECUTE stmt3;
END IF;
END WHILE;
CLOSE cur1;
END;

1 个答案:

答案 0 :(得分:0)

您有两种选择: (还有更多,但我认为这是你想要的)

  1. 在inicialization中用单引号括起值:

    SET a1 = "'iOS'";
    SET a2 = "'Apple'";
    SET a3 = "'iPhone'";
    SET a4 = "'iOS X.X'";
    

    并修正句子:

    CONCAT('UPDATE ', @data_all_group, ' SET `os` = ',a1,', `phone_manufacture` = ',a2,', `phone_model` = ',a3,', `os_vers` = ',a4)
    

    结果将是:

    UPDATE your_table SET `os` = 'iOS', `phone_manufacture` = 'Apple', `phone_model` = 'iPhone', `os_vers` = 'iOS X.X'
    
  2. OR:修复查询构建:

     SET @s = CONCAT('UPDATE ', @data_all_group
       , "SET `os` = '" , a1 ,"',"
       , "`phone_manufacture` = '",a2,"',"
       , "`phone_model` = '",a3,"',"
       , "`os_vers` = '",a4,"'");