在Oracle中若删除一个不存在的表,如 “DROP TABLE tableName”,则会提示:
ORA-00942:表或视图不存在
若在程序中执行该语句则会报异常,这就需要我们在删除表前先判断该表是否存在,若存在则删除.
1
2
3
4
5
6
7
8
9
10
11
|
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”:标识符无效
若在程序中执行该语句则会报异常,这就需要我们在删除字段前先判断该字段是否存在,若存在则删除.
1
2
3
4
5
6
7
8
9
10
11
12
|
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 ; |