Postgresql generator column is causing a syntax error

I’m adding a generator column to a Postgresql v14.8 table using pgAdmin4 with this expression:

CASE WHEN columnA > 0 THEN columnA / columnB

But am getting this syntax error:

syntax error at or near ")" LINE 16: …> 0 THEN columnA / columnB)
STORED;^

I’ve looked at multiple examples and can’t identify what I’ve done wrong.

EDIT

Full SQL code as requested:

ALTER TABLE IF EXISTS public."table_name_here"
    ADD COLUMN columnA integer GENERATED ALWAYS AS (
       CASE WHEN columnA > 0 THEN columnA / columnB
) STORED;

>Solution :

Use the following code to create a generated column in PostgreSQL 15.4; simply replace "your_table" with the real name of your table.

ALTER TABLE your_table
ADD COLUMN generated_column numeric GENERATED ALWAYS AS (
    CASE WHEN columnA > 0 THEN columnA / columnB ELSE NULL END
);

Leave a Reply