functions for triggers: passing parameters

Started by Nicoabout 25 years ago3 messagesgeneral
Jump to latest
#1Nico
nicod@tiscalinet.it

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)

#2Doug McNaught
doug@wireboard.com
In reply to: Nico (#1)
Re: functions for triggers: passing parameters

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

#3Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Nico (#1)
Re: functions for triggers: passing parameters

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)