Re: problem with inserting a generated seq no into not null field - Mailing list pgsql-general

From Manfred Koizar
Subject Re: problem with inserting a generated seq no into not null field
Date
Msg-id a4dbc0hqsk2l1khvhp3qd37rt44hij2cd3@email.aon.at
Whole thread Raw
In response to problem with inserting a generated seq no into not null field  (mike <mike@bristolreccc.co.uk>)
List pgsql-general
On Tue, 08 Jun 2004 12:37:48 +0100, mike <mike@bristolreccc.co.uk>
wrote:
>INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
>WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE
>max(file_no)+1 END FROM tb_files GROUP BY (subj||area),subj,area HAVING
>subj = '4' AND area = '8');

If there is no row with subj = '4' AND area = '8', then SELECT ...
HAVING returns no rows and therefore max(file_no) is NULL.  And NULL+1
is NULL.  Try

    INSERT INTO tb_files (subj, area, file_no)
    SELECT '4', '8', coalesce(max(file_no), 0)+1
      FROM tb_files
     WHERE subj = '4' AND area = '8';

Servus
 Manfred

pgsql-general by date:

Previous
From: Jan Wieck
Date:
Subject: Re: The pgreplication project
Next
From: Bruno Wolff III
Date:
Subject: Re: Index on points