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

From: Postgres User <postgres(dot)developer(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using Insert - Default in a condition expression ??
Date: 2009-09-29 11:04:46
Message-ID: b88c3460909290404o788134e8uc6cdbcdb4ddd5b4f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>> >
>> > 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 Kretschmer

Again, this approach works for a simple example. But for a larger
function with any number of input params and multiple columns with
default values, it's not practical for an ORM code generator.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message tomrevam 2009-09-29 11:23:14 query is taking longer time after a while
Previous Message Sam Mason 2009-09-29 10:53:14 Re: bulk inserts