From: | Vincent Veyron <vv(dot)lists(at)wanadoo(dot)fr> |
---|---|
To: | Postgres User <postgres(dot)developer(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Indicating DEFAULT values in INSERT statement |
Date: | 2011-08-12 18:31:06 |
Message-ID: | 1313173866.3306.13.camel@asus-1001PX.home |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Le mardi 09 août 2011 à 15:57 -0700, Postgres User a écrit :
>
>
> From a db function, I'd like to force the use of default when an input
> parameter is null.
May be something like this :
CREATE TABLE users (
id bigint NOT NULL,
username text NOT NULL,
is_active boolean DEFAULT false
);
CREATE FUNCTION new_user (text, boolean default 't') RETURNS SETOF users
AS $$
INSERT INTO users(username, is_active) VALUES($1, $2) RETURNING *;
$$ LANGUAGE SQL;
select new_user('no_status_defined');
new_user
--------------------------
(10,no_status_defined,t)
(1 ligne)
As you can see, in the case where the second parameter is absent, the
default set by the function (true) is used.
http://www.postgresql.org/docs/9.0/static/xfunc-sql.html#XFUNC-SQL-PARAMETER-DEFAULTS
It won't work if NOT NULL is set on the field.
--
Vincent Veyron
http://marica.fr/
Logiciel de gestion des sinistres et des contentieux pour le service juridique
From | Date | Subject | |
---|---|---|---|
Next Message | David Johnston | 2011-08-12 18:32:02 | Re: COPY from .csv File and Remove Duplicates |
Previous Message | Merlin Moncure | 2011-08-12 18:17:16 | Re: Functions returning setof record -- can I use a table type as my return type hint? |