How to create an index using a procedure in Oracle

How can we create an index using a procedure?

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