当前位置: 代码迷 >> Oracle开发 >> oracle动态给表命名有关问题
  详细解决方案

oracle动态给表命名有关问题

热度:30   发布时间:2016-04-24 07:28:53.0
oracle动态给表命名问题
declare
  ncount number;
begin
  select count(*) into ncount from user_tables 
  where table_name = 'STAT_1DAY_DOMAIN_' || 2424;
  if ncount>0 then
  execute immediate 'drop table STAT_1DAY_DOMAIN_2424';  
  end if;
end;
第一个方法测试使用成功

declare
  ncount number;
  begin
  select count(*) into ncount from user_tables 
  where table_name = 'STAT_1DAY_DOMAIN_' || user_tables;
  if ncount>0 then
  execute immediate 'drop table' 'STAT_1DAY_DOMAIN_' || user_tables; end if;
  end;
我将上面第二个方法放入到存储过程中进行对动态表名的操作但是删除表名的语句提示表名错误,应该如何改写

------解决方案--------------------
SQL code
--创建表create table STAT_1DAY_DOMAIN_test(id int);/select * from STAT_1DAY_DOMAIN_test;/--创建sp--http://topic.csdn.net/u/20120530/11/3a88bd75-c7b5-4c8e-afe2-80b6872df06a.html?124create or replace procedure sp_dropTable(v_tbname in varchar2)asv_sql varchar2(4000);begin     v_sql:='drop table STAT_1DAY_DOMAIN_'||v_tbname;     execute immediate v_sql;end sp_dropTable;/--执行spbeginsp_dropTable('test') ; end;/--查询表--select * from STAT_1DAY_DOMAIN_test;
------解决方案--------------------
SQL code
--创建表create table STAT_1DAY_DOMAIN_test(id int);/select * from STAT_1DAY_DOMAIN_test;/--创建spcreate or replace procedure sp_dropTable(v_tbname in varchar2)asv_sql varchar2(4000);begin     v_sql:='drop table STAT_1DAY_DOMAIN_'||v_tbname;     execute immediate v_sql;     --异常处理     EXCEPTION           when others then           dbms_output.put_line('table not exists');     end sp_dropTable;/--执行spbeginsp_dropTable('test') ; end;/--查询表--select * from STAT_1DAY_DOMAIN_test;
  相关解决方案