Re: rules

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Martín Marqués <martin(at)bugs(dot)unl(dot)edu(dot)ar>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: rules
Date: 2001-04-26 21:24:18
Message-ID: 200104262124.QAA04341@jupiter.jw.home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Martín Marqués wrote:
> Is it posible to make a rule execute more then one query?
>
> Something like:
>
> CREATE RULE rule_name AS ON INSERT TO table1
> DO INSTEAD
> INSERT INTO table2 VALUES
> (new.value1,new.value2)
> INSERT INTO table3 VALUES
> (x,y)

Yes:

CREATE RULE rule_name AS ON INSERT TO table1
DO INSTEAD (
INSERT INTO table2 VALUES
(new.value1,new.value2);
INSERT INTO table3 VALUES
(x,y);
);

You just omitted the parens and semicoli :-)

>
> If not, is there a way to do this? Triggers maybe?

Triggers too (even if yes above and effectively you haven't
asked for):

CREATE FUNCTION whatever () RETURNS opaque AS '
BEGIN
INSERT INTO table2 VALUES
(new.value1,new.value2);
INSERT INTO table3 VALUES
(...);
RETURN NULL; -- returning NULL from a BEFORE trigger
-- suppresses the triggering INSERT to
-- happen.
END;'
LANGUAGE 'plpgsql';

CREATE TRIGGER table1_ins BEFORE INSERT ON table1
FOR EACH ROW EXECUTE whatever();

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

In response to

  • rules at 2001-04-26 14:21:48 from Martín Marqués

Browse pgsql-sql by date

  From Date Subject
Next Message postgres 2001-04-26 21:46:56 Using Transaction Blocks w/ SELECT
Previous Message Joel Burton 2001-04-26 20:53:47 Re: rules