rgbcof
asked on
sql, check if column exists before adding
I am using postgres. I want to check to see if a column exists before adding a column. But when I tried "if not exists (select * from mytable.firstname) begin alter table mytable add column firstname character varying end"
I got a syntax error:
ERROR: syntax error at or near "if"
I got a syntax error:
ERROR: syntax error at or near "if"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It didn't work for Postgres
Are you just using that line or are there lines before that line?
ASKER
IF not exists (select * from mytable.firstname) then alter table mytable add column firstname character varying ; END IF;
ERROR: syntax error at or near "if"