如何判断一个表是否存在

crazymanlgm 2005-09-15 11:22:50
如何判断一个表是否存在,如果存在,需要进行一些操作,我在存在过程中是这样写的

if exists(select * from user_tables where table_name = 'TABLE1') then
drop table 'TABLE1';
end if;

首先非权限错误
...全文
254 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
feng2 2005-09-15
  • 打赏
  • 举报
回复
SQL> select decode(count(0),0,'不存在','存在') T from user_tables where table_name = 'A';

T
------
存在

Executed in 0.016 seconds

SQL> select decode(count(0),0,'不存在','存在') T from user_tables where table_name = 'AA';

T
------
不存在

Executed in 0.016 seconds

SQL>
yufeiyxl 2005-09-15
  • 打赏
  • 举报
回复
v_count number;
select count(*) into v_count from tab where tname=upper('table_name');
if v_count = 1 then
......
end if;
didu811 2005-09-15
  • 打赏
  • 举报
回复
利用oracle中数据字典中的ALL_CATALOG视图可以查询到库中All tables, views, synonyms, sequences accessible to the user的信息

现在我们利用它来查询SYSTEM用户下是否存在表'T_SYS_USER'
select * from ALL_CATALOG t
where t.owner='SYSTEM'
and t.table_type='TABLE'
and t.table_name='T_SYS_USER'
注意:'SYSTEM' 、'TABLE'、'T_SYS_USER'都必须是大写
通过ALL_CATALOG视图可以很准确的判断一个表是否存在
waterfirer 2005-09-15
  • 打赏
  • 举报
回复
假设p_table是表名变量,定义一个字符串变量v_strsql
if exists(select 1 from tab where tname = upper(p_table)) then
v_strsql:='drop table' || p_table --拼sql
execute immediate v_strsql;
end if;
crazymanlgm 2005-09-15
  • 打赏
  • 举报
回复
都不行
超叔csdn 2005-09-15
  • 打赏
  • 举报
回复
if exists(select 1 from tab where tname = upper('TABLE1')) then
drop 'TABLE1';
end if;
hevin 2005-09-15
  • 打赏
  • 举报
回复
试试这样:

if exists(select 1 from user_tables where table_name = 'TABLE1') then
drop table 'TABLE1';
end if;

不过与你的写法是一个意思来的,我怀疑是不是你运行存储过程的用户与TABLE1的用户不是同一个

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧