drop_table.sql 686 B

1234567891011121314151617181920212223242526
  1. CREATE PROCEDURE `drop_table`(
  2. IN a_name VARCHAR(64)
  3. ) BEGIN
  4. DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  5. ROLLBACK;
  6. GET DIAGNOSTICS CONDITION 1 @error = MESSAGE_TEXT;
  7. SET @error = CONCAT('Failed to remove table `',a_name,'`. ',@error);
  8. RESIGNAL SET MESSAGE_TEXT = @error;
  9. END;
  10. SET @name = a_name;
  11. IF EXISTS (
  12. SELECT 1
  13. FROM INFORMATION_SCHEMA.TABLES
  14. WHERE TABLE_SCHEMA = DATABASE()
  15. AND TABLE_NAME = @name
  16. ) THEN
  17. SET @sql = CONCAT('DELETE FROM `',@name,'`;');
  18. PREPARE stmt FROM @sql;
  19. EXECUTE stmt;
  20. DEALLOCATE PREPARE stmt;
  21. END IF;
  22. SET @sql = CONCAT('DROP TABLE IF EXISTS `',@name,'`;');
  23. PREPARE stmt FROM @sql;
  24. EXECUTE stmt;
  25. DEALLOCATE PREPARE stmt;
  26. END;