日期:2014-05-16  浏览次数:20471 次

遍历表进行操作
   最近,工作中有个需求,需要删除表结构,重新导入数据。在俊哥指导下,有了以下的sql脚本,记录之。

declare

  t1 user_tables%rowtype;    
  t2 varchar2(100);                                      
begin                                                               
  for t1 in (select * from user_tables ORDER BY TABLE_NAME)                        
  loop
        t2 := 'truncate table EBAY_LOMBARDI7_DEV_DB_PROC06.' ||t1.table_name|| ';';
       
      dbms_output.put_line( t2);

  end loop;
end;

在删表的时候,时常会有以下的报错:

Error starting at line 15 in command:
drop table EBAY_LOMBARDI7_DEV_DB_PROC06.LSW_BPD
Error report:
SQL Error: ORA-02449: unique/primary keys in table referenced by foreign keys
02449. 00000 -  "unique/primary keys in table referenced by foreign keys"
*Cause:    An attempt was made to drop a table with unique or
           primary keys referenced by foreign keys in another table.
*Action:   Before performing the above operations the table, drop the
           foreign key constraints in other tables. You can see what
           constraints are referencing a table by issuing the following
           command:
           SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";

那么需要加上级联删除,例如:
drop table [schema_name].[table_name] cascade constraints purge;