Oracle数据库对象失效


项目中开发使用了VPD,数据库用户B的对象的创建依赖于数据用户A,由于用户A的对象进行DDL、迁移或dump等操作,造成了用户B的对象INVALID。应用系统的数据源使用了用户B,因此造成应用系统出错。

此时可进行如下处理:

1,找到失效的对象

  1. select object_type,object_id,object_name  
  2. from user_objects   
  3. where status='INVALID' order by object_type  
2,进行判断后,可以重新编译这些对象。

编译的方法有多种:

1) DBMS_DDL

2.)DBMS_UTILITY
3.)UTL_RECOMP
4)UTLRP.SQL
5)Manually Recompile


最佳方案是手动编译这些对象,可以参考下面的SQL脚本:

  1. Spool recompile.sql  
  2.   
  3. Select ‘alter ‘object_type’ ’object_name’ compile;’  
  4. From user_objects  
  5. Where status <> ‘VALID’  
  6. And object_type IN (‘VIEW’,’SYNONYM’,  
  7. PROCEDURE’,’FUNCTION’,  
  8. ‘PACKAGE’,’TRIGGER’);  
  9.   
  10. Spool off  
  11. @recompile.sql  
  12.   
  13.   
  14. Note: VIEW,SYNONYM,PROCEDURE,PACKAGE,FUNCTION,TRIGGER  
  15.   
  16.   
  17. Spool pkg_body.sql  
  18.   
  19. Select ‘alter package ’object_name’ compile body;’  
  20. From user_objects  
  21. where status <> ‘VALID’  
  22. And object_type = ‘PACKAGE BODY’;  
  23.   
  24. Spool off  
  25. @pkg_body.sql  
  26.   
  27.   
  28. Spool undefined.sql  
  29.   
  30. select ‘alter materizlized view ’object_name’ compile;’  
  31. From user_objects  
  32. where status <> ‘VALID’  
  33. And object_type =‘UNDEFINED’;  
  34.   
  35. Spool off  
  36. @undefined.sql  
  37.   
  38.   
  39. Spool javaclass.sql  
  40.   
  41. Select ‘alter java class ’object_name’ resolve;’  
  42. from user_objects  
  43. where status <> ‘VALID’  
  44. And object_type =‘JAVA CLASS’;  
  45.   
  46. Spool off  
  47. @javaclass.sql  
  48.   
  49.   
  50. Spool typebody.sql  
  51.   
  52. Select ‘alter type ‘object_name’ compile body;’  
  53. From user_objects  
  54. where status <> ‘VALID’  
  55. And object_type =‘TYPE BODY’;  
  56.   
  57. Spool off  
  58. @typebody.sql  
  59.   
  60.   
  61. Spool public_synonym.sql  
  62.   
  63. Select ‘alter public synonym ‘object_name’ compile;’  
  64. From user_objects  
  65. Where status <> ‘VALID’  
  66. And owner = ‘PUBLIC’  
  67. And object_type = ‘SYNONYM’;  
  68.   
  69. Spool off  
  70. @public_synonym.sql  

相关内容