In a table, I want to check if a particular column exists or not. If the column does not exist, I want to alter the table and create that column.
I am using Oracle 11g.
Now we use the below query to check the existence of a column. Query: IF COL_LENGTH('table_name','column_name') IS NOT NULL PRINT 'Column Exists'; ELSE PRINT 'Column does not Exists';
Another method to find if the column exists in a table is by using COL_LENGTH system function. This function returns the length of the column if it exists in the table. If not, it will return NULL.
Try this:
declare p_count NUMBER;
select count(1) int p_count
from ALL_TAB_COLUMNS
where OWNER = '<SCHEMA_NAME>'
and TABLE_NAME = '<TABLE_NAME>'
and COLUMN_NAME = '<COLUMN_NAME>';
IF p_count = 0 THEN
--add your column
END IF;
Eventually (depending on the rights) You can use user_tab_columns
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With