Thread: using queries as default value?

using queries as default value?

From
Don Patou
Date:
is it possible to have a query as a column default value. I have a statistic
table and I would like one of its column to do like "select count(colx) from
anothertable".
What's the best way to do this?

thanx in advance

Re: using queries as default value?

From
Josh Berkus
Date:
Don,

> is it possible to have a query as a column default value. I have a statistic
> table and I would like one of its column to do like "select count(colx) from
> anothertable".
> What's the best way to do this?

Add a Trigger to the table.   See Triggers in the online docs, and also under
the PL/pgSQL docs (under "Procedural Languages").

--
-Josh Berkus
 Aglio Database Solutions
 San Francisco


Re: using queries as default value?

From
Nabil Sayegh
Date:
Am Sam, 2003-05-31 um 00.04 schrieb Don Patou:
> is it possible to have a query as a column default value. I have a statistic
> table and I would like one of its column to do like "select count(colx) from
> anothertable".
> What's the best way to do this?

Maybe what you want is a VIEW.
But that would always do the query _at runtime_, not while inserting.

HTH
--
 e-Trolley Sayegh & John, Nabil Sayegh
 Tel.: 0700 etrolley /// 0700 38765539
 Fax.: +49 69 8299381-8
 PGP : http://www.e-trolley.de


Re: using queries as default value?

From
Tom Lane
Date:
Don Patou <pknoob@noos.fr> writes:
> is it possible to have a query as a column default value.

Not directly, but you can fake it by hiding the query in an SQL or PL
function that you call in the DEFAULT expression.

I agree with the nearby replies questioning whether this is really what
you want to do at all ... but if it's, that's how.

            regards, tom lane

Re: using queries as default value?

From
Don Patou
Date:
> Not directly, but you can fake it by hiding the query in an SQL or PL
> function that you call in the DEFAULT expression.

I tried this:

CREATE FUNCTION somefunc() RETURNS integer AS '
BEGIN
    select count(colx) from anothertable;
END;
is it the right syntax?
also, how do I call my function as a default value, do I just need to "default
somefunc()" at the end of the column?

one more question, I couldn't find in the documentation how to dump databases
in a text file. could u give me the syntax?

thanx in advance