Re: conditional IF statements in postgresql - Mailing list pgsql-general

From David G Johnston
Subject Re: conditional IF statements in postgresql
Date
Msg-id 1404704839864-5810691.post@n5.nabble.com
Whole thread Raw
In response to conditional IF statements in postgresql  (Madhurima Das <madhurima.das@gmail.com>)
Responses Re: conditional IF statements in postgresql  (Andy Colson <andy@squeakycode.net>)
List pgsql-general
madhu_d wrote
> 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??

Not by any logic that I find recognizable.  It is also absolutely not
syntactically correct.

I have no clue why you think the updating of the column is conditional.  I
can understand needing to add a missing column before you can effect an
update but that can and should be independent of the need to update the
column.

Neither "IF" nor "UPDATE TABLE ... ADD" are valid commands that you can
issue directly via PQExec.

The only valid commands are listed here:

http://www.postgresql.org/docs/9.3/interactive/sql-commands.html

Any other commands, of which conditionals are a subset, must be executed
within the context of a DO command or user-defined function.  In particular
you should see if pl/pgsql can be made to accomplish that which you need.

David J.




--
View this message in context:
http://postgresql.1045698.n5.nabble.com/conditional-IF-statements-in-postgresql-tp5810687p5810691.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: David Wall
Date:
Subject: Re: pg_dump slower than pg_restore
Next
From: Andreas Joseph Krogh
Date:
Subject: text-prefix search in 9.4's JSONB