RULE - special variables?

Started by Matthew Peterover 19 years ago6 messagesgeneral
Jump to latest
#1Matthew Peter
survivedsushi@yahoo.com

Do rules get special variables like triggers? Can I set variables in them like
triggers?

____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail.
http://new.mail.yahoo.com

#2Richard Huxton
dev@archonet.com
In reply to: Matthew Peter (#1)
Re: RULE - special variables?

Matthew Peter wrote:

Do rules get special variables like triggers? Can I set variables in them like
triggers?

You get NEW/OLD but they mean something subtly different. Rules act on a
query-tree so they are more like placeholders.

You can't set variables in triggers. You do so in a function. If your
rule calls a function, that could have variables.

Spend some time playing with rules - make sure you understand the
difference between them and triggers.
--
Richard Huxton
Archonet Ltd

#3Albe Laurenz
all@adv.magwien.gv.at
In reply to: Richard Huxton (#2)
Re: RULE - special variables?

Do rules get special variables like triggers? Can I set
variables in them like
triggers?

A rule is one or more SQL-Statements - how do you set variables in SQL?

Don't think of a rule as a program!

In a statement in a rule, you have the pseudotables 'OLD' and 'NEW'
which refer to the affected rows of the table on which the rule is
defined, OLD (in DELETE and UPDATE rules) referring to the values before
the triggering statement takes place, and NEW (in INSERT and UPDATE)
referring to the values afterwards.

Yours,
Laurenz Albe

#4Shane Ambler
pgsql@007Marketing.com
In reply to: Albe Laurenz (#3)
Re: RULE - special variables?

Albe Laurenz wrote:

Do rules get special variables like triggers? Can I set
variables in them like
triggers?

A rule is one or more SQL-Statements - how do you set variables in SQL?

SET [ SESSION | LOCAL ] name { TO | = } { value | 'value' | DEFAULT }

http://www.postgresql.org/docs/8.1/interactive/sql-set.html

--

Shane Ambler
pgSQL@007Marketing.com

Get Sheeky @ http://Sheeky.Biz

#5Richard Huxton
dev@archonet.com
In reply to: Shane Ambler (#4)
Re: RULE - special variables?

Shane Ambler wrote:

Albe Laurenz wrote:

Do rules get special variables like triggers? Can I set variables in
them like
triggers?

A rule is one or more SQL-Statements - how do you set variables in SQL?

SET [ SESSION | LOCAL ] name { TO | = } { value | 'value' | DEFAULT }

http://www.postgresql.org/docs/8.1/interactive/sql-set.html

That's configuration settings, not user-defined variables though.

--
Richard Huxton
Archonet Ltd

#6Matthew Peter
survivedsushi@yahoo.com
In reply to: Richard Huxton (#2)
Re: RULE - special variables?
--- Richard Huxton <dev@archonet.com> wrote:

Matthew Peter wrote:

Do rules get special variables like triggers? Can I set variables in them like
triggers?

You get NEW/OLD but they mean something subtly different. Rules act on a
query-tree so they are more like placeholders.

You can't set variables in triggers. You do so in a function. If your
rule calls a function, that could have variables.

You can set variables in triggers in the DECLARE block when plpgsql is used.

Reading the docs they only show single-statement rules so it never said assigning
could be done so here I am. Sometimes it's easier to write a quick rule than a
trigger function and separate firing condition, especially when they function so
similiarily in theory. Thanks.

____________________________________________________________________________________
Cheap talk?
Check out Yahoo! Messenger's low PC-to-Phone call rates.
http://voice.yahoo.com