?
Solved

sql, check if column exists before adding

Posted on 2011-10-10
4
Medium Priority
?
948 Views
Last Modified: 2012-06-27
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"
0
Comment
Question by:rgbcof
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
karl-henrik earned 1000 total points
ID: 36941076
IF not exists (select * from mytable.firstname) then alter table mytable add column firstname character varying ; END IF;
0
 

Author Comment

by:rgbcof
ID: 36943306
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"
0
 

Author Closing Comment

by:rgbcof
ID: 36946831
It didn't work for Postgres
0
 
LVL 3

Expert Comment

by:karl-henrik
ID: 36946952
Are you just using that line or are there lines before that line?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Integration Management Part 2
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question