I have procedure that I want to run regularly to update whatever Oracle needs in order to function efficently. This procedure computes statistics, updates and rebuilds indexes on tables that are built nightly.
It looks for tables and indexes that begin with a pattern and builds the update statistics, rebuild indexes, etc command with the intent that I would to execute immediate .
In the past I got an ORA error saying I or it lacked permission to run, but I'm wondering if I can just directly execute these things.
begin
-- Would add EXECUTE immediate lsql below, but there is some permission issue
-- -execute immediate lsql;
for sc_hub_indexes_record in hub_indexes_cursor
loop
lsql:='alter index '||sc_hub_indexes_record.table_owner||'.'||sc_hub_indexes_record.index_name||' rebuild online parallel;';
dbms_output.put_line(lsql);
execute immediate lsql;
lsql:='DBMS_STATS.GATHER_INDEX_STATS(ownname => ''SC_REPORT_NEW'', indname => '||apostrophe||sc_hub_indexes_record.index_name||apostrophe||';';
dbms_output.put_line(lsql);
execute immediate lsql;
end loop;
for sc_hub_tables_record in hub_tables_cursor
loop
quoted_owner := apostrophe||'SC_REPORT_NEW'||apostrophe;
quoted_table := apostrophe||sc_hub_tables_record.table_name||apostrophe;
lsql:='dbms_stats.gather_table_stat ( ownname => '||quoted_owner||', tabname => '||quoted_table||', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE );';
dbms_output.put_line(lsql);
-- This gives an error for lack of permission. Not yet sure why.
execute immediate lsql;
end loop;
end;
Please note, my account would work if I just ran the begin alter index;end;/ type things, it only failed if I made an execute statement out of it. However, I don't know how else to dynamically build these statements.
execute immediate
. Call it directlyDBMS_STATS.GATHER_INDEX_STATS(ownname => 'SC_REPORT_NEW', indname => sc_hub_indexes_record.index_name);
– Wernfried Domscheitalter
keyword so it must useexecute immediate
/dbms_sql
. – William Robertson