Re: Indicating DEFAULT values in INSERT statement

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 20:31:24
Message-ID: 1313181084.3306.14.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 serial 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andy Colson 2011-08-12 20:36:23 Re: Kudos
Previous Message Vick Khera 2011-08-12 20:24:01 Re: Postgres on SSD