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;
相关推荐
lklong 2020-11-22
oraclemch 2020-11-06
shilukun 2020-10-10
周嘉笙 2020-11-09
iilegend 2020-10-19
EricRay 2020-10-16
zhuzhufxz 2020-09-16
dataminer 2020-08-17
bfcady 2020-08-16
Hody 2020-08-16
FightFourEggs 2020-08-16
数据库设计 2020-08-16
Seandba 2020-08-16
Omega 2020-08-16
zjyzz 2020-08-16
yanghuatong 2020-08-16
ktyl000 2020-08-16
dbasunny 2020-08-16