最近expdp迁移数据后,大量视图同义词失效,单个编译麻烦死,生成存储过程批量编译,方便,快捷。
1.批量编译存储工程的存储过程
create or replace procedure compile_invalid_procedures(
p_owner varchar2 -- 所有者名称,即 SCHEMA
) as
--编译某个用户下的无效存储过程
str_sql varchar2(200);
begin
for invalid_procedures in (select object_name from all_objects
where status = 'INVALID' and object_type = 'PROCEDURE' and owner=upper(p_owner))
loop
str_sql := 'alter procedure '||p_owner||'.'||invalid_procedures.object_name || ' compile';
begin
execute immediate str_sql;
exception
--When Others Then Null;
when OTHERS Then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
2.批量编译视图的存储过程
create or replace procedure compile_invalid_views(
p_owner varchar2 -- 所有者名称,即 SCHEMA
) as
--编译某个用户下的无效视图
str_sql varchar2(200);
begin
for invalid_views in (select object_name from all_objects
where status = 'INVALID' and object_type = 'VIEW' and owner=upper(p_owner))
loop
str_sql := 'alter view '||p_owner||'.' ||invalid_views.object_name || ' compile';
begin
execute immediate str_sql;
exception
--When Others Then Null;
when OTHERS Then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
3.批量编译同义词的存储过程
create or replace procedure compile_invalid_synonyms(
p_owner varchar2 -- 所有者名称,即 SCHEMA
) as
--编译某个用户下的无效同义词
str_sql varchar2(200);
begin
for invalid_synonyms in (select object_name from all_objects
where status = 'INVALID' and object_type = 'SYNONYM' and owner=upper(p_owner))
loop
str_sql := 'alter synonym '||p_owner||'.' ||invalid_synonyms.object_name || ' compile';
begin
execute immediate str_sql;
exception
--When Others Then Null;
when OTHERS Then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
4.批量编译函数的存储过程
create or replace procedure compile_invalid_functions(
p_owner varchar2 -- 所有者名称,即 SCHEMA
) as
--编译某个用户下的无效存储过程
str_sql varchar2(200);
begin
for invalid_procedures in (select object_name from all_objects
where status = 'INVALID' and object_type = 'FUNCTION' and owner=upper(p_owner))
loop
str_sql := 'alter function '||p_owner||'.'||invalid_procedures.object_name || ' compile';
begin
execute immediate str_sql;
exception
--When Others Then Null;
when OTHERS Then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;