Re: conditional IF statements in postgresql

From: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: conditional IF statements in postgresql
Date: 2014-07-07 03:47:19
Message-ID: 1404704839864-5810691.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Joseph Krogh 2014-07-07 09:28:34 text-prefix search in 9.4's JSONB
Previous Message David Wall 2014-07-07 03:38:33 Re: pg_dump slower than pg_restore