Link to home
Start Free TrialLog in
Avatar of rgbcof
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"
ASKER CERTIFIED SOLUTION
Avatar of karl-henrik
karl-henrik
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgbcof
rgbcof

ASKER

Still syntax error.
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"
Avatar of rgbcof

ASKER

It didn't work for Postgres
Are you just using that line or are there lines before that line?