update / exist column Postgresql

josef

i have a table abonnement with a column topic and other column. i want to update the value of the column topic. But before updating, i want to verify if this column exist or not to use this script with other developers in my team. If exist, I do update, else, i must to create this column and after do update. Here is my script :

IF EXISTS (select topic from abonnement) 
then 
update abonnement set topic ='valeurTopic'; 
else 
ALTER TABLE abonnement ADD COLUMN topic character varying; 
update abonnement set topic= 'valeurTopic';
end if;

I had an error:

ERREUR:  erreur de syntaxe sur ou près de « IF »
LINE 1: IF EXISTS (SELECT topic
    ^

Any solution please ?

cungiderm

I would change the logic:

DO $$ 
    BEGIN
        BEGIN
            ALTER TABLE <table_name> ADD COLUMN <column_name> <column_type>;
        EXCEPTION
            WHEN duplicate_column THEN 
            UPDATE <table_name> SET <column_name> = 'valeurTopic';
    END;
END; $$

I got the code from How to add column if not exists on PostgreSQL?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Column does not Exist in postgresql?

From Dev

PostgreSQL query -- column does not exist

From Dev

PostgreSQL "Column "foo" does not exist"

From Dev

Postgresql Column Doesn't Exist

From Dev

Update Count column in Postgresql

From Dev

update column of a table in PostgreSQL

From Dev

Update join on postgresql - Relation does not exist

From Dev

Doctrine column id does not exist on PostgreSQL

From Dev

PostgreSQL sql command error column 'does not exist'

From Dev

PostgreSQL: Error: column of relation does not exist

From Dev

Column doesn't exist in PostgreSQL View

From Dev

PostgreSQL "column does not exist" error in CartoDB

From Dev

Postgresql slow update for a new column

From Dev

update vachar column to date in postgreSQL

From Dev

PostgreSQL increment serial column on update

From Dev

PostgreSQL complex update operation on column

From Dev

Postgresql Update a unique column and validate before Update

From Java

How to ignore a column update if not exist using knex?

From Dev

Update a column based on other column with postgresql

From Dev

Update column only if value doesnt exist in other column

From Dev

How to update first NULL column if not exist in another column

From Dev

Update enum column in Laravel migration using PostgreSQL

From Dev

Postgresql - update column even if value is too long

From Dev

How to update a jsonb column's field in PostgreSQL?

From Dev

Shift (update) unique column values in PostgreSQL

From Dev

PostgreSQL update column with JOIN over 3 tables

From Dev

Update column of a table based on array of varchar in PostgreSQL

From Dev

Postgresql - update column even if value is too long

From Dev

Update Column Field ONLY If Empty (PostgreSQL)

Related Related

HotTag

Archive