473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

plpgsql trigger function with arguments

I'm sure I saw something like this on the postgresql web
site but the the search function is down in the documentation
area.

I'm unable to pass a function arguments in a CREATE
TRIGGER statement.

What am I doing wrong here?

CREATE FUNCTION pregs_func (VARCHAR(15))
RETURNS trigger
LANGUAGE plpgsql
AS '
DECLARE
trigger_type ALIAS FOR $1;

BEGIN
IF trigger_type = ''insert'' THEN
...
END IF;
END;
';

CREATE TRIGGER pregs_insert_trigger
AFTER INSERT
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('insert');

CREATE TRIGGER pregs_update_trigger
AFTER UPDATE
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('update');

The CREATE TRIGGER statements return:
ERROR: CreateTrigger: function pregs_func() does not exist
I tried doing variations on:
create trigger pregs_insert_trigger after insert on pregs
for each row execute procedure
pregs_func (cast('insert' as varchar(15)));

thinking that I don't have the right function because the
datatypes don't match, but I get:
ERROR: parser: parse error at or near "cast" at character 106

PostgreSQL 7.3.

Thanks,

Karl <ko*@meme.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #1
2 5498
hello

You have to write functions without params. Params for triggers are
accessed not via function params, but via special variable TG_ARGV[]

http://archives.postgresql.org/pgsql...3/msg01021.php

regards
Pavel Stehule

On Tue, 2 Mar 2004, Karl O. Pinc wrote:
I'm sure I saw something like this on the postgresql web
site but the the search function is down in the documentation
area.

I'm unable to pass a function arguments in a CREATE
TRIGGER statement.

What am I doing wrong here?

CREATE FUNCTION pregs_func (VARCHAR(15))
RETURNS trigger
LANGUAGE plpgsql
AS '
DECLARE
trigger_type ALIAS FOR $1;

BEGIN
IF trigger_type = ''insert'' THEN
...
END IF;
END;
';

CREATE TRIGGER pregs_insert_trigger
AFTER INSERT
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('insert');

CREATE TRIGGER pregs_update_trigger
AFTER UPDATE
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('update');

The CREATE TRIGGER statements return:
ERROR: CreateTrigger: function pregs_func() does not exist
I tried doing variations on:
create trigger pregs_insert_trigger after insert on pregs
for each row execute procedure
pregs_func (cast('insert' as varchar(15)));

thinking that I don't have the right function because the
datatypes don't match, but I get:
ERROR: parser: parse error at or near "cast" at character 106

PostgreSQL 7.3.

Thanks,

Karl <ko*@meme.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #2
On Tue, 02 Mar 2004 21:35:27 -0600, Karl O. Pinc wrote:

<..>>
CREATE TRIGGER pregs_insert_trigger
AFTER INSERT
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('insert');

CREATE TRIGGER pregs_update_trigger
AFTER UPDATE
ON pregs FOR EACH ROW
EXECUTE PROCEDURE pregs_func('update');


According to the docs, this is unnecessary. A trigger function
automatically has a number of variables instantiated for it:

http://www.postgresql.org/docs/7.3/s...plpgsqltrigger

The TG_OP variable contains 'INSERT','UPDATE' or 'DELETE'.

--
Colin Fox
President
CF Consulting Inc.

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josué Maldonado | last post by:
Hi list, That's the question, anyone knows a way to do that? Thanks in advance, Josue Maldonado.
6
by: Martin Marques | last post by:
We are trying to make some things work with plpgsql. The problem is that I built several functions that call one another, and I thought that the way of calling it was just making the assign: ...
3
by: Philippe Lang | last post by:
Hello, How can open a unix shell from a PLPGSQL function / trigger? I would like to send an email from a postgres database, and also send some unix commands to the server, through the...
10
by: lnd | last post by:
After copied pg database from one PC to another -I could not find plpgsql function(s) in the copied database. -had to instal plpgsql language handler again -whilst tables and data moved...
2
by: Karl O. Pinc | last post by:
I'd like to write: SELECT larger(colA, colB) FROM foo and am wondering the best way to go about it. (Really, I'd like the larger() function to take an arbitrary number of arguments but I...
14
by: Karl O. Pinc | last post by:
Hi, Thought perhaps some other eyes than mine can tell if I'm doing something wrong here or if there's a bug somewhere. I've never passed a ROWTYPE varaible to a function but I don't see where...
2
by: Mark Cave-Ayland | last post by:
Hi everyone, I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2 that given a tree node id (ictid) will return all the nodes below it in the tree, one row per node. When I try...
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
1
by: Stefano Bonnin | last post by:
CORRECTION, even if I define a stupid "hello world" trigger function the problem is present: for example CREATE FUNCTION sns_action () RETURNS TRIGGER AS ' BEGIN RAISE NOTICE ''hello...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.