How To Check for an Index in Oracle

前端 未结 1 1778
囚心锁ツ
囚心锁ツ 2021-01-04 01:18

I am writing a schema upgrade script for a product that depends on an Oracle database. In one area, I need to create an index on a table - if that index does not already ex

相关标签:
1条回答
  • 2021-01-04 02:07

    select count(*) from user_indexes where index_name = 'myIndex'

    sqlplus won't support IF..., though, so you'll have to use anonymous PL/SQL blocks, which means EXECUTE IMMEDIATE to do DDL.

    DECLARE
        i INTEGER;
    BEGIN
        SELECT COUNT(*) INTO i FROM user_indexes WHERE index_name = 'MYINDEX';
        IF i = 0 THEN
            EXECUTE IMMEDIATE 'CREATE INDEX myIndex ...';
        END IF;
    END;
    /
    

    Edit: as pointed out, Oracle stores unquoted object names in all uppercase.

    0 讨论(0)
提交回复
热议问题