Re: Using Insert - Default in a condition expression ??

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Using Insert - Default in a condition expression ??
Date: 2009-09-29 06:54:22
Message-ID: 20090929065422.GC2463@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Sim Zacks :
> >
> > I'm trying to write an INSERT INTO statement that will use a DEFAULT
> > value when an input parameter is null.
> >
> Neither of my 2 methods are pretty.
> 1) Use a trigger.
> 2) Grab and cast the default value from the information_schema.columns
> view and plug it in.
>
> Another option is to build your insert sql as a string and then execute it.

Another option:

rewrite your function:

CREATE OR REPLACE FUNCTION "name_add" (p_name varchar, p_created_date
date) RETURNS integer AS $$ BEGIN case when $2 is null then INSERT INTO
names(name, created_date) values (p_name, default); else insert into
names values ($1, $2); end case; return 1; end; $$ language plpgsql;

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2009-09-29 07:29:40 Re: sync structures
Previous Message Postgres User 2009-09-29 06:53:37 Re: Using Insert - Default in a condition expression ??