DROPPROCEDURE idata;delimiter;;createprocedure idata()begindeclare i int;set i=1;while(i<=100000)doinsertinto t values(i, i, i);set i=i+1;endwhile;end;;delimiter;call idata();
删除索引
-- drop index 的存储过程:当存在时删除,因为 drop index 不支持 if existsDROPPROCEDUREIFEXISTS drop_index_if_exists;DELIMITER//createprocedure drop_index_if_exists(IN p_db varchar(200),IN p_tablename varchar(200),IN p_idxname VARCHAR(200))beginDECLARE str VARCHAR(250);set@str=concat(' drop index ',p_idxname,' on ',p_tablename);selectcount(*)into@cntfrom information_schema.statisticswhere table_name=p_tablename and index_name=p_idxname and TABLE_SCHEMA = p_db;if@cnt>0thenPREPARE stmt FROM@str;EXECUTE stmt ;endif;END//DELIMITER;
删除列
-- drop column 的存储过程:当存在时删除,因为 drop column 不支持 if existsDROPPROCEDUREIFEXISTS drop_column_if_exists;DELIMITER//createprocedure drop_column_if_exists(IN p_db varchar(200),IN p_tablename varchar(200),IN p_columnname VARCHAR(200))beginDECLARE str VARCHAR(250);set@str=concat(' alter table ',p_tablename,' drop column ',p_columnname);selectcount(*)into@cntfrom information_schema.columnswhere table_name=p_tablename and column_name=p_columnname and TABLE_SCHEMA = p_db;if@cnt>0thenPREPARE stmt FROM@str;EXECUTE stmt ;endif;END//DELIMITER;
条件不满足立刻返回
DELIMITER//createprocedure check_duplicate()
label:beginselectcount(*)into@cntfrom(selectcount(*)from dc_fb_form_tables groupby`season_uuid`,`team_uuid`,`round_uuid`,`group_uuid`havingcount(*)>1orderbynull) t;select concat('check dc_fb_form_tables count is ',@cnt,',');if@cnt>0thenselect'fail';LEAVE label;endif;selectcount(*)into@cntfrom(selectcount(*)from dc_fb_injuries groupby`person_uuid`,`start_date`havingcount(*)>1orderbynull) t;select concat('check dc_fb_injuries count is ',@cnt,',');if@cnt>0thenselect'fail';LEAVE label;endif;selectcount(*)into@cntfrom(selectcount(*)from dc_fb_match_event groupby`match_uuid`,`event_type`,`code`,`person_uuid`,`team_uuid`,`minute`havingcount(*)>1orderbynull) t;select concat('check dc_fb_match_event count is ',@cnt,',');if@cnt>0thenselect'fail';LEAVE label;endif;END//DELIMITER;
delimiter ;;create procedure idata()begin declare i int; set i=1; while(i<=100000)do insert into t values(i, i, i); set i=i+1; end while;end;;delimiter ;call idata();