Thread: rules

rules

From
Martín Marqués
Date:
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)

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

Saludos... :-)

-- 
El mejor sistema operativo es aquel que te da de comer.
Cuida tu dieta.
-----------------------------------------------------------------
Martin Marques                  |        mmarques@unl.edu.ar
Programador, Administrador      |       Centro de Telematica                      Universidad Nacional
        del Litoral
 
-----------------------------------------------------------------


Re: rules

From
Joel Burton
Date:
On Thu, 26 Apr 2001, [iso-8859-1] 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)

test=# \h create rule
Command:     CREATE RULE
Description: Defines a new rule
Syntax:
CREATE RULE name AS ON event   TO object [ WHERE condition ]   DO [ INSTEAD ] action

where action can be:

NOTHING
|
query
|
( query ; query ... )
|
[ query ; query ... ]

ie

CREATE RULE snog AS ON UPDATE TO foo DO INSTEAD ( ...; ... );

-- 
Joel Burton   <jburton@scw.org>
Director of Information Systems, Support Center of Washington



Re: rules

From
Jan Wieck
Date:
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
froma 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@Yahoo.com #



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