insert with select as value

From: Milos Prudek <prudek(at)bvx(dot)cz>
To: pgsql-general(at)postgresql(dot)org
Subject: insert with select as value
Date: 2004-06-22 14:22:33
Message-ID: 40D840A9.5050102@bvx.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I need to insert a value = max(value)+1, where max is a select limited
by a 'where' clause. Like this:

INSERT INTO table (idthread, idsection,txt)
VALUES (
(SELECT max(idthread)+1 FROM table WHERE idsection = 'CZE'), 'CZE',
'sample text')
);

This works fine, except when the result of SELECT is empty - which is
true when the table is empty.

Is it possible to create a "SELECT max(idthread)+1 FROM table WHERE
idsection = 'CZE';" that will return value 1 instead of value None if
the SELECT has no results?

--
Milos Prudek
_________________
Most websites are
confused chintzy gaudy conflicting tacky unpleasant... unusable.
Learn how usable YOUR website is! http://www.spoxdesign.com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Marcelo Soares 2004-06-22 14:37:40 Re: insert with select as value
Previous Message Tom Lane 2004-06-22 14:05:28 Re: User Privileges using dblink