当前位置: 代码迷 >> Oracle技术 >> 怎么使用存储过程删除多个表
  详细解决方案

怎么使用存储过程删除多个表

热度:46   发布时间:2016-04-24 08:28:19.0
如何使用存储过程删除多个表
找了相关资料,都好像只是针对一个表的操作,现在我想在一个存储过程中,用一个循环删除(delete or truncate)多个有一个规则的用户表(A_table1,A_table2...),并且有异常的处理机制或回滚,有路过的大虾请赐教一下啊!

------解决方案--------------------
拼字符串 execute immediate 执行

http://www.cnblogs.com/huanghai223/archive/2011/06/29/2093660.html
------解决方案--------------------
SQL code
--Oracle Code--建议你把这些表明存储到一个表里面create or replace procedure updatetablebegin     cursor tp_cur is select 表名 from 存储表名的表;tp_sql varchar2(400);        for lp_cur in tp_cur loop                dbms_output.put_line('now begin deleting table '||lp_cur.table_name);                tp_sql:='delete '||lp_cur.table_name||' where col_name....';          _____此处自己更改                execute immediate tp_sql;                dbms_output.put_line('finish deleting table '||lp_cur.table_name);                commit;        end loop;end;
------解决方案--------------------
SQL code
--要回滚的话 你在 我写的commit处 加判断--查询要删除的这些表的表名的时候 你自己加判断
------解决方案--------------------
探讨
还不是很清楚,能不能针对truncate table讲详细一点,最好有一些注释

------解决方案--------------------
SQL code
create or replace procedure proc_DealTablesas  sSql varchar2(2000);  cursor c1 is  select Object_name from user_objects where Object_type='TABLE' ;--and otherswhere; 查询出你要删除的表名begin  for c2 in c1 loop    sSql := ' delete from '||c2.table_name;    begin      execute immediate sSql;    exception      when others then        rollback;    end;  end loop;  end;
------解决方案--------------------
truncate 是把表数据都清空了,而且回滚不便。

还是老实的用 delete 吧。

另外,删除一张表、多张表 没什么区别。 一条一条执行就是了,最多加个事务、回滚点之类的。
从业务上考虑,先标识(需要删除的数据),最后统一删除,更保险些。
------解决方案--------------------
探讨
truncate 是把表数据都清空了,而且回滚不便。

还是老实的用 delete 吧。

另外,删除一张表、多张表 没什么区别。 一条一条执行就是了,最多加个事务、回滚点之类的。
从业务上考虑,先标识(需要删除的数据),最后统一删除,更保险些。

------解决方案--------------------
在存储过程使用嵌套表,把查询的表名放入嵌套表,然后使用循环删除表

当然也可以创建一个脚本文件删除表中数据
------解决方案--------------------
+1
探讨
SQL code

create or replace procedure proc_DealTables
as
sSql varchar2(2000);
cursor c1 is select Object_name from user_objects where Object_type='TABLE' ;--and otherswhere; 查询出你要删除的表名
begi……

------解决方案--------------------
探讨
拼字符串 execute immediate 执行

http://www.cnblogs.com/huanghai223/archive/2011/06/29/2093660.html

------解决方案--------------------
表能随便的drop,truncate么?
把你所要删除的表放到一个配置表中,写个存储过程,开游标将表名一个一个的带出来,动态语句删除
------解决方案--------------------
SQL code
select 'truncate table'||table_name||','from user_tableswhere .......
------解决方案--------------------
探讨
SQL code

create or replace procedure proc_DealTables
as
sSql varchar2(2000);
cursor c1 is select Object_name from user_objects where Object_type='TABLE' ;--and otherswhere; 查询出你要删除的表名
begi……
  相关解决方案