Re: Subqueries

From: "Thomas F(dot) O'Connell" <tfo(at)sitening(dot)com>
To: Bob Pawley <rjpawley(at)shaw(dot)ca>
Cc: Postgre General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Subqueries
Date: 2005-11-07 05:55:09
Message-ID: CF9D4574-FCDC-4A1F-A81C-8B53A5AC9FB9@sitening.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Nov 6, 2005, at 3:19 PM, Bob Pawley wrote:

> Last week I received help form the list in inserting a serial row
> of one table (process) into a row in a table called specification.
>
> I was able to expand that to include inserting the same information
> into a table called pipe.
> -------------------
> create or replace function base() returns trigger as $$
> begin
> insert into specification (fluid_id) values
> (new.fluid_id);
> insert into pipe (fluid_id) values (new.fluid_id);
> return null;
> end;
> $$ language plpgsql;
>
> create trigger trig1 after insert on process
> for each row execute procedure base();
> ----------------
> This works well.
>
> Now I want to modify the program so that only those rows from
> process that have ip in the column ip_op_reactor are inserted into
> pipe.
>
> Following is my best result after studying every piece of
> documentation available to me.
>
> ------------------
> create or replace function base() returns trigger as $$
> begin
> insert into specification (fluid_id) values
> (new.fluid_id);
>
> select fluid_id as fi from process pr;
> select ip_op_reactor as iop from pr ;
> select fluid_id as fi from pipe pi
> (select fi
> from pr where iop = 'ip',
>
> insert into pi (fi) values (new.fi));
>
> return null;
> end;
> $$ language plpgsql;
>
> create trigger trig1 after insert on process
> for each row execute procedure base();
> -----------------
> This is the error I get.
> ------------------
> ERROR: SELECT query has no destination for result data
> HINT: If you want to discard the results, use PERFORM instead.
> CONTEXT: PL/pgSQL function "base" line 4 at SQL statement
> ----------------
> I would very much appreciate any help as to where I am going wrong.
>
> Thanks

Might be helpful to re-read the chapter on Basic Statements in PL/pgSQL:

http://www.postgresql.org/docs/8.0/static/plpgsql-statements.html

For SELECTs in PL/pgSQL, you either need a target (via INTO) or you
need to use PERFORM instead.

That's what the HINT is about. It thinks because you're not
specifying a target for your SELECTs that you might want to discard
the results.

If (as I assume) you don't, you'll probably also want to declare
variables to serve as targets for the results of your SELECTs.

--
Thomas F. O'Connell
Database Architecture and Programming
Co-Founder
Sitening, LLC

http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-469-5150
615-469-5151 (fax)

In response to

  • Subqueries at 2005-11-06 21:19:55 from Bob Pawley

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2005-11-07 06:05:14 Re: How to create a virtual column
Previous Message Klint Gore 2005-11-07 02:26:50 Re: Connect to a database in a .sql file