Thread: functions for triggers: passing parameters

functions for triggers: passing parameters

From
Nico
Date:
What is wrong?

CREATE FUNCTION set_value(text) RETURNS OPAQUE AS '
    DECLARE
    val ALIAS FOR $1;
    BEGIN
    NEW.inf := val;
        RETURN NEW;
    END;
' LANGUAGE 'plpgsql';


CREATE TABLE t1 (
    id serial,
    info text
    );
CREATE TRIGGER t1_set_val BEFORE INSERT ON t1
FOR EACH ROW EXECUTE PROCEDURE set_value('some info');

============================
psql 7.1beta5 result:


CREATE
psql:function_for_trigger.sql:15: NOTICE:  CREATE TABLE will create implicit
sequence 't1_id_seq' for SERIAL column 't1.id'
psql:function_for_trigger.sql:15: NOTICE:  CREATE TABLE/UNIQUE will create
implicit index 't1_id_key' for table 't1'
CREATE
psql:function_for_trigger.sql:17: ERROR:  CreateTrigger: function set_value()
does not exist
test=#

(the language plpgsql is already loaded)

Re: functions for triggers: passing parameters

From
Doug McNaught
Date:
Nico <nicod@tiscalinet.it> writes:

> What is wrong?

[...]

> CREATE TRIGGER t1_set_val BEFORE INSERT ON t1
> FOR EACH ROW EXECUTE PROCEDURE set_value('some info');

Trigger functions can't take arguments.

-Doug

Re: functions for triggers: passing parameters

From
Stephan Szabo
Date:
On Fri, 9 Mar 2001, Nico wrote:

> What is wrong?
>
> CREATE FUNCTION set_value(text) RETURNS OPAQUE AS '
>     DECLARE
>     val ALIAS FOR $1;
>     BEGIN
>     NEW.inf := val;
>         RETURN NEW;
>     END;
> ' LANGUAGE 'plpgsql';
>
>
> CREATE TABLE t1 (
>     id serial,
>     info text
>     );
> CREATE TRIGGER t1_set_val BEFORE INSERT ON t1
> FOR EACH ROW EXECUTE PROCEDURE set_value('some info');
>
> ============================
> psql 7.1beta5 result:
>
>
> CREATE
> psql:function_for_trigger.sql:15: NOTICE:  CREATE TABLE will create implicit
> sequence 't1_id_seq' for SERIAL column 't1.id'
> psql:function_for_trigger.sql:15: NOTICE:  CREATE TABLE/UNIQUE will create
> implicit index 't1_id_key' for table 't1'
> CREATE
> psql:function_for_trigger.sql:17: ERROR:  CreateTrigger: function set_value()
> does not exist
> test=#
>
> (the language plpgsql is already loaded)

Right, because triggers take arguments differently.  Trigger functions
must return opaque and take no arguments.  Arguments passed at create
trigger time are passed in via TG_ARGV[] (number in TG_NARGS i believe)