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 ?
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.
Comments