How to Reset an MySQL AutoIncrement using a MAX value from another table?

前端 未结 8 688
遥遥无期
遥遥无期 2020-12-08 07:04

I know this won\'t work, tried it in various forms and failed all times. What is the simplest way to achieve the following result?

ALTER TABLE XYZ AUTO_INCRE         


        
8条回答
  •  心在旅途
    2020-12-08 07:18

    Who is having problem with PREPARE stmt FROM 'ALTER TABLE XYZ AUTO_INCREMENT = ?' can use

    CREATE PROCEDURE reset_xyz_autoincrement
    BEGIN
          SELECT @max := MAX(ID)+ 1 FROM ABC; 
          set @alter_statement = concat('ALTER TABLE temp_job_version AUTO_INCREMENT = ', @max);
          PREPARE stmt FROM @alter_statement;
          EXECUTE stmt;
          DEALLOCATE PREPARE stmt;
    END
    

提交回复
热议问题