Oracle删除表、字段之前判断表、字段是否存在

在oracle中若删除一个不存在的表,如 “drop table tablename”,则会提示:

ora-00942:表或视图不存在

若在程序中执行该语句则会报异常,这就需要我们在删除表前先判断该表是否存在,若存在则删除.

declare
num number;
begin
select count(1)
into num
from user_tables
where table_name = upper('tablename');
if num > 0 then
execute immediate 'drop table tablename';
end if;
end;

在oracle中若删除表中一个不存在的字段,如 “alter table test drop column xxx”,则会提示:

ora-00904:”xxx”:标识符无效

若在程序中执行该语句则会报异常,这就需要我们在删除字段前先判断该字段是否存在,若存在则删除.

declare
num number;
begin
select count(1)
into num
from cols
where table_name = upper('tablename')
and column_name = upper('columnname');
if num > 0 then
execute immediate 'alter table tablename drop column columnname';
end if;
end;

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐