CREATE PROCEDURE `DropIndexIfExists`(
IN i_table_name VARCHAR(128),
IN i_index_name VARCHAR(128)
)
BEGIN
SET @tableName = i_table_name;
SET @indexName = i_index_name;
SET @indexExists = 0;
SELECT
1
INTO @indexExists FROM
INFORMATION_SCHEMA.STATISTICS
WHERE
TABLE_NAME = @tableName
AND INDEX_NAME = @indexName;
SET @query = CONCAT(
'DROP INDEX ', @indexName, ' ON ', @tableName
);
IF @indexExists THEN
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END
create procedure DeleteIndex()
begin
IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_NAME = 'TableName'
AND INDEX_NAME = 'IndexName' AND INDEX_SCHEMA='DbName') THEN
ALTER TABLE TableName DROP index THead2;
END IF;
END
2条答案
按热度按时间pbossiut1#
我没有看到任何直接使用
IF EXISTS
的DROP INDEX
方法。作为一种变通方法,我编写了下面的过程,它对我很有效。g52tjvyc2#
试试这个