Advertisement
AmourSpirit

SQL - SP - drop_index_if_exists

Jul 12th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.63 KB | None | 0 0
  1. DELIMITER $$
  2. CREATE PROCEDURE `drop_index_if_exists`(IN `theTable` VARCHAR(128) CHARSET utf8, IN `theIndexName` VARCHAR(128) CHARSET utf8) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
  3.     IF((SELECT COUNT(*) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name =
  4.     theTable AND index_name = theIndexName) > 0) THEN
  5.    SET @s = CONCAT('DROP INDEX `' , theIndexName , '` ON `' , theTable, '`');
  6.    PREPARE stmt FROM @s;
  7.    EXECUTE stmt;
  8.  END IF;
  9. END$$
  10. DELIMITER ;
  11.  
  12. -- PROCEDURE TO THAT DROPS INDEX IF THE EXIST
  13. -- USAGE: CALL drop_index_if_exists('TABLE_NAME','INDEX_NAME');
  14. -- MySql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement