Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:55:27+00:00 2026-06-15T18:55:27+00:00

I have the following stored procedure (Taken from a comment on http://dev.mysql.com/doc/refman/5.0/en/create-index.html , used

  • 0

I have the following stored procedure (Taken from a comment on http://dev.mysql.com/doc/refman/5.0/en/create-index.html, used to check if an index exists on a table):

DELIMITER $$
DROP PROCEDURE IF EXISTS `create_index_if_not_exists`$$

CREATE DEFINER=`user`@`%` PROCEDURE `create_index_if_not_exists`(table_name_vc varchar(50), index_name_vc varchar(50), field_list_vc varchar(200))
SQL SECURITY INVOKER
BEGIN

set @Index_cnt = (
select  count(1) cnt
FROM    INFORMATION_SCHEMA.STATISTICS
WHERE   table_name = table_name_vc
and index_name = index_name_vc
);

IF ifnull(@Index_cnt,0) = 0 THEN set @index_sql = concat('Alter table ',table_name_vc,' ADD INDEX ',index_name_vc,'(',field_list_vc,');');

PREPARE stmt FROM @index_sql;
EXECUTE stmt;

DEALLOCATE PREPARE stmt;

END IF;

END$$
DELIMITER ;

The strange thing is that it works perfectly on CentOS 5.4 with MySQL 5.5.25, but does not work on Mac OS X 10.8.1 with MySQL 5.5.24. On Mac, the @Index_cnt is always 0 if I add

select @Index_cnt

in the stored procedure. If I do the SELECT COUNT(1) cnt ... statement on it’s own, then 1 or 0 is returned as it should be.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:55:28+00:00Added an answer on June 15, 2026 at 6:55 pm

    I have re-written the stored procedure and this now works on all platforms:

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS myschema.create_index_if_not_exists $$
    CREATE PROCEDURE myschema.create_index_if_not_exists(in p_tableName VARCHAR(128), in p_indexName VARCHAR(128), in p_columnName VARCHAR(128) )
    BEGIN
    
    PREPARE stmt FROM 'SELECT @indexCount := COUNT(1) from information_schema.statistics WHERE `table_name` = ? AND `index_name` = ?';
    SET @table_name = p_tableName;
    SET @index_name = p_indexName;
    EXECUTE stmt USING @table_name, @index_name;
    DEALLOCATE PREPARE stmt;
    
    -- select @indexCount;
    
    IF( @indexCount = 0 ) THEN
      SELECT 'Creating index';
      SET @createIndexStmt = CONCAT('CREATE INDEX ', p_indexName, ' ON ', p_tableName, ' ( ', p_columnName ,')');
      PREPARE stmt FROM @createIndexStmt;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
    END IF;
    
    END $$
    
    DELIMITER ;
    

    Use it as follows:

    call myschema.create_index_if_not_exists('MyTable','end_time_index','end_time');
    

    This was tested on MAC OS X 10.8.2 with MySQL 5.5.24 and on Windows 7 with MySQL 5.5.21

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following stored procedure: CREATE PROCEDURE testProc(IN p_idProject INTEGER) BEGIN DECLARE nowTime DATETIME;
I have the following stored procedure that I have to get data from: EXECUTE
I am using Oracle 10g and I have the following stored procedure: CREATE OR
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I have the following stored procedure to retrieve a new datetime: CREATE PROCEDURE [dbo].[GetTransactionTime]
I have the following stored procedure in an SQL Server 2005 database (meant simply
I have the following stored procedure which will generate mon to sun and then
I have the following PL/SQL stored procedure on an Oracle database: PROCEDURE MyProcedure (
I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND
I have the following iBatis mapping for an Oracle Stored Procedure that returns a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.