To put DDL in a PL / SQL block, you will need to use dynamic SQL.
Personally, I would check if the column exists first, and then produces a DDL. Sort of
DECLARE l_cnt INTEGER; BEGIN SELECT COUNT(*) INTO l_cnt FROM dba_tab_cols WHERE table_name = 'MY_TABLE' AND owner = <<owner of table>> AND column_name = 'COL_NAME'; IF( l_cnt = 0 ) THEN EXECUTE IMMEDIATE 'ALTER TABLE my_table ADD( col_name VARCHAR2(100) )'; ELSE EXECUTE IMMEDIATE 'ALTER TABLE my_table MODIFY( col_name VARCHAR2(100) )'; END IF; END;
If you do not have access to DBA_TAB_COLS , you can also use ALL_TAB_COLS or USER_TAB_COLS , depending on which schema is in the table and what rights you have in the table.
source share