Thread: Question regarding triggers

Question regarding triggers

From
Dmitri Fuerle
Date:
 
   I'm writing a trigger but running into problems.  My problem is that I can not determine anyway to tell what fields are in the *new* record.  Without knowing what fields are there I get runtime errors if that's not what is being updated example:
 
CREATE FUNCTION "public"."check_shipment" () RETURNS trigger AS'
begin
    If new.shipment_type_id = 4 then
        --do something
    end if;
    return new;
end;
 
in the above example everything works if shipment_type_id is being updated.  If it is not I will receive a run-time error because shipment_type_id is not part of the record new.
 
Please help,
Dmitri
 


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

Re: Question regarding triggers

From
achill@matrix.gatewaynet.com
Date:
Why dont you try to write your trigger in C?

On Mon, 20 Oct 2003, Dmitri Fuerle wrote:

>  
>    I'm writing a trigger but running into problems.  My problem is that I can not determine anyway to tell what
fieldsare in the *new* record.  Without knowing what fields are there I get runtime errors if that's not what is being
updatedexample:
 
>  
> CREATE FUNCTION "public"."check_shipment" () RETURNS trigger AS'
> begin
>     If new.shipment_type_id = 4 then
>         --do something
>     end if;
>     return new;
> end;
>  
> in the above example everything works if shipment_type_id is being updated.  If it is not I will receive a run-time
errorbecause shipment_type_id is not part of the record new.
 
>  
> Please help,
> Dmitri
>  
> 
> 
> 
> ---------------------------------
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search

-- 
-Achilleus



Re: Question regarding triggers

From
"A.Bhuvaneswaran"
Date:
> Why dont you try to write your trigger in C?

Hi, one cannot write triggered procedures in C. Currently, it can only be
written in plpsgql.

> > CREATE FUNCTION "public"."check_shipment" () RETURNS trigger AS'
> > begin
> >     If new.shipment_type_id = 4 then
> >         --do something
> >     end if;
> >     return new;
> > end;
> >  in the above example everything works if shipment_type_id is being
> > updated.  If it is not I will receive a run-time error because
> > shipment_type_id is not part of the record new.

All the fields of updated record must available in NEW variable.  Refer
the manual for details. Forward your sql & run-time error for further 
assistance.

regards,
bhuvaneswaran




Re: Question regarding triggers

From
achill@matrix.gatewaynet.com
Date:
On Mon, 20 Oct 2003, A.Bhuvaneswaran wrote:

> > Why dont you try to write your trigger in C?
> 
> Hi, one cannot write triggered procedures in C. Currently, it can only be
> written in plpsgql.

Where did you get that impression from?

Do an
SELECT tgrelid,tgfoid,proname from pg_trigger,pg_proc where 
tgfoid=pg_proc.oid and prolang=13;
in your system to check if you have any :)

> 
> > > CREATE FUNCTION "public"."check_shipment" () RETURNS trigger AS'
> > > begin
> > >     If new.shipment_type_id = 4 then
> > >         --do something
> > >     end if;
> > >     return new;
> > > end;
> > >  in the above example everything works if shipment_type_id is being
> > > updated.  If it is not I will receive a run-time error because
> > > shipment_type_id is not part of the record new.
> 
> All the fields of updated record must available in NEW variable.  Refer
> the manual for details. Forward your sql & run-time error for further 
> assistance.
> 
> regards,
> bhuvaneswaran
> 
> 

-- 
-Achilleus



Re: Question regarding triggers

From
Tom Lane
Date:
"A.Bhuvaneswaran" <bhuvan@symonds.net> writes:
>> Why dont you try to write your trigger in C?

> Hi, one cannot write triggered procedures in C.

Oh?  All the built-in trigger procedures are.
        regards, tom lane


Re: Question regarding triggers

From
Dmitri Fuerle
Date:
  Thanks for the help.  I discovered my error while coming up with a better example.
 
 
Thanks,
  Dmitri


"A.Bhuvaneswaran" <bhuvan@symonds.net> wrote:
> Why dont you try to write your trigger in C?

Hi, one cannot write triggered procedures in C. Currently, it can only be
written in plpsgql.

> > CREATE FUNCTION "public"."check_shipment" () RETURNS trigger AS'
> > begin
> > If new.shipment_type_id = 4 then
> > --do something
> > end if;
> > return new;
> > end;
> > in the above example everything works if shipment_type_id is being
> > updated. If it is not I will receive a run-time error because
> > shipment_type_id is not part of the record new.

All the fields of updated record must available in NEW variable. Refer
the manual for details. Forward your sql & run-time error for further
assistance.

regards,
bhuvaneswaran


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search