conditional IF statements in postgresql

From: Madhurima Das <madhurima(dot)das(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: conditional IF statements in postgresql
Date: 2014-07-07 03:32:10
Message-ID: CAMsahj3X2euJb-fjAtD7EfgbJXRgAwZj8rqiJDEdbW0Yoi2_Qg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I am writing a C program to access a PostgreSQL database, where

I add a column if it doesn't exists in the table

or, update the column, if the column already exits.

Please suggest how to work with the conditional statements.

Thanks!

N.B. I wrote the following:

res = PQexec(conn, "IF COL_LENGTH('protein_sequence','comment') IS NULL");
PQclear(res);
if(res)
{
res = PQexec(conn, "ALTER TABLE protein_sequence ADD comment
VARCHAR(500)");
PQclear(res);
}
else
{
res = PQexec(conn, "UPDATE TABLE protein_sequence ADD comment
VARCHAR(500)");
PQclear(res);
}

Is the code logically correct??

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Wall 2014-07-07 03:38:33 Re: pg_dump slower than pg_restore
Previous Message Tom Lane 2014-07-06 16:06:39 Re: pg_dump slower than pg_restore