oracle创建表以前判断是否已经存在

2025-03-24 08:03:13
推荐回答(2个)
回答1:

主要是查询all_tables表的TABLE_NAME和OWNER,如果表存在,则执行execute immediate 'drop table TABLE_NAME';Sql代码--判断表是否存在,如果存在则删除declarenum number;beginselect count(1) into num from all_tables where TABLE_NAME = 'EMP' and OWNER='SCOTT'; if num=1 then execute immediate 'drop table EMP';end if;end;/--创建表CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7, 2), COMM NUMBER(7, 2), 可以将上述存储过程加载到每一个create table前面。

回答2:

当前用户下是否有某个表
select count(*) from user_tables where table_name = 'TABLE_NAME';
某个用户下是否有某个表?
select count(*) from dba_tables where owner = 'USER_NAME' and table_name = 'TABLE_NAME';