How to create an index using a procedure in Oracle
How can we create an index using a procedure?
You'll have to use dynamic SQL and the EXECUTE IMMEDIATE command to create the index in the procedure. Such a stored procedure may look like the following:
CREATE PROCEDURE my_pro AS stmt VARCHAR2(200); BEGIN stmt := 'CREATE INDEX my_index ON my_table(columnZ)'; EXECUTE IMMEDIATE stmt; END; /From the above, it should be easy to modify the code for your situation.
I would caution you that creating objects on the fly is normally considered a bad practice and is usually the result of a poor database design. So make sure you have a well-designed database schema before implementing this procedure.
Dig Deeper on Oracle database administration
Related Q&A from Brian Peasland
Calling stored procedures inside user-defined functions in Oracle Database
In this expert answer, Brian Peasland explains how to call a stored procedure inside user-defined functions in Oracle Database. Continue Reading
Copy Oracle Database but with no data
One reader asks how he can make a copy of his Oracle Database but not have any data in it, for test purposes. Continue Reading
How to add disk to ASM diskgroup with two-way mirroring
Learn how to add disk to ASM diskgroup with two-way mirroring and how to specify a failgroup in this tip from Oracle expert Brian Peasland. Continue Reading