terrydiederich2
1/10/2019 - 2:51 PM

Add Index only if it doesn't exist

select if (
    (
    SELECT COUNT(1) IndexIsThere 
      FROM INFORMATION_SCHEMA.STATISTICS
     WHERE table_schema=DATABASE() 
       AND table_name='vendor_po_received' 
       AND index_name='ndx_MaterialID_ReceiveDate'
    ) > 0
    ,'select 1;'
    ,'ALTER TABLE vendor_po_received ADD INDEX ndx_MaterialID_ReceiveDate(material_id, fkvendor_po, complete, receive_date);') INTO @tmp;
PREPARE stmt1 FROM @tmp;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;