我谷歌它,但我找不到我需要的东西。
我应该使用PREPARE
, EXECUTE
in 在单个语句中执行多个更新查询MySQL
。
示例查询:
update tableName set column2='a', column3='b' where column1=1;
update tableName set column2='c', column3='d' where column1=2;
update tableName set column2='f', column3='g' where column1=3;
SET @Query=myUpdateQuery;
PREPARE stmt FROM @Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
我尝试使用上述查询,但遇到错误
错误代码:1064 您的 SQL 语法有错误;
我正在努力克服这个错误。
请让我知道如何完成我的需求。提前致谢。
试试这个。您必须将必须更新的 id 的逗号分隔列表作为过程的第一个参数传递。希望这可以帮助。
DELIMITER $$
CREATE PROCEDURE proc_name(IN strIDs VARCHAR(255))
BEGIN
DECLARE strLen INT DEFAULT 0;
DECLARE SubStrLen INT DEFAULT 0;
IF strIDs IS NULL THEN
SET strIDs = '';
END IF;
do_this:
LOOP
SET strLen = LENGTH(strIDs);
update tableName
set column2 = 'a', column3 = 'b'
where column1 = SUBSTRING_INDEX(strIDs, ',', 1);
SET SubStrLen = LENGTH(SUBSTRING_INDEX(strIDs, ',', 1));
SET strIDs = MID(strIDs, SubStrLen, strLen);
IF strIDs = NULL THEN
LEAVE do_this;
END IF;
END LOOP do_this;
END
$$
DELIMITER ;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句