在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;