Is it databases in general, SQL or Postgresql? - Mailing list pgsql-general

From Bob Pawley
Subject Is it databases in general, SQL or Postgresql?
Date
Msg-id 006801c5ea09$f3d0f490$ac1d4318@OWNER
Whole thread Raw
Responses Re: Is it databases in general, SQL or Postgresql?  (Jaime Casanova <systemguards@gmail.com>)
Re: Is it databases in general, SQL or Postgresql?  (Bricklen Anderson <BAnderson@PresiNET.com>)
Re: Is it databases in general, SQL or Postgresql?  (Alban Hertroys <alban@magproductions.nl>)
List pgsql-general
Hope someone can help me learn.
 
Sample 1 below, does work. It transfers every serial number generated by table pr into table pi with no duplication.
 
Sample 2 below, does not work. From a logical (perhaps naive) extension of Sample 1, I adapted the function to identify which of the serial numbers in table pr is to be transferred to table pi. I am attempting to do this as part of the database structure not as data retrieval.
 
Could someone explain to me why this isn't acceptable as a simple basic function?
 
Could someone explain to me what needs to be changed, enhanced or modified to make this database structure work?
 
Thanks in advance.
 
Bob
 

Sample 1

 

create table pr

(Process_Name            varchar (60) not null, Fluid_ID serial, Fluid varchar (30) not null, contain  varchar (3),

            constraint  pr_pk primary key (fluid_id));

 

create table pi

            (process_name varchar (60), Fluid_ID int4 not null, Fluid varchar (30),

            contain varchar (3),

            constraint pi_pk primary key (fluid_id),

            constraint pi_fluid_id foreign key (fluid_id)

            references pr (fluid_id) );

 

            create or replace function base() returns trigger as $$

            begin

            insert into pi (fluid_id) values (new.fluid_id);

            return null;

            end;

            $$ language plpgsql;

 

            create trigger trig1 after insert on pr

            for each row execute procedure base();

 

            insert into pr (process_name, fluid, contain)

            values ('boiler_water', 'water','ip');

---------------------------------

Sample 2

 

 

            create table pr

            (Process_Name varchar (60) not null, Fluid_ID serial, Fluid varchar (30) not null, contain  varchar (3),

            constraint  pr_pk primary key (fluid_id));

 

            create table pi

            (process_name varchar (60), Fluid_ID int4 not null, Fluid varchar (30),

            contain varchar (3),

            constraint pi_pk primary key (fluid_id),

            constraint pi_fluid_id foreign key (fluid_id)

            references pr (fluid_id) );

 

            create or replace function base() returns trigger as $$

            begin

            insert into pi (fluid_id) values (new.fluid_id)

            where  pr (contain)  = 'ip';

            return null;

            end;

            $$ language plpgsql;

 

            create trigger trig1 after insert on pr

            for each row execute procedure base();

 

            insert into pr (process_name, fluid, contain)

            values ('boiler_water', 'water','ip');

 

Error Message –

 

ERROR:  syntax error at or near "where" at character 41

QUERY:  insert into pi (fluid_id) values ( $1 ) where pr (contain) = 'ip'CONTEXT:  PL/pgSQL function "base" line 2 at SQL statement

pgsql-general by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: Restore a PG database in Windows
Next
From: Bruce Momjian
Date:
Subject: Re: Number of items in a cursor...