初始化清空表便於生產環境
CREATE DEFINER=`root`@`%` PROCEDURE `reset_autoIncrement`()
begin
declare v_table_name varchar(100); -- 自定義變數
declare done int default false; -- 自定義控制遊標迴圈變數,預設false
declare sql_for_select varchar(500);
declare my_cursor cursor for
select table_name
from information_schema.tables
where table_schema=database()
and table_name not like 'ACT%'
and table_name not like 'QRTZ%'
and table_name not like 'sys%'
and table_name != 'erp_goods' ;
declare continue handler for not found set done = true; -- 繫結控制變數到遊標,遊標迴圈結束自動轉true
open my_cursor; -- 開啟遊標
myloop: loop -- 開始迴圈體,
fetch my_cursor into v_table_name; -- 將遊標當前讀取行的資料順序賦予自定義變數
set sql_for_select = concat("truncate table ", v_table_name);
#select sql_for_select;
set @sql = sql_for_select;
prepare stmt from @sql; -- 預處理動態sql語句
execute stmt ; -- 執行sql語句
#修改自增id
set sql_for_select = concat( "alter table ", v_table_name," auto_increment = 1");
set @sql = sql_for_select;
prepare stmt from @sql; -- 預處理動態sql語句
execute stmt ; -- 執行sql語句
if done then -- 判斷是否繼續迴圈
leave myloop; -- 結束迴圈
end if;
end loop myloop; -- 結束自定義迴圈體
deallocate prepare stmt; -- 釋放prepare
close my_cursor; -- 關閉遊標
end