Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql General > Re: Is this pos...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 8 Topic 15474 of 17437
Post > Topic >>

Re: Is this possible in a trigger?

by kreno@[EMAIL PROTECTED] ("Kerri Reno") May 6, 2008 at 07:13 PM

------=_Part_10618_27563894.1210122809277
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

This is easy with plpython.  We do something similar.

Kerri

On Tue, May 6, 2008 at 6:10 PM, Klint Gore <kgore4@[EMAIL PROTECTED]
> wrote:

> Fernando wrote:
>
> > I want to keep a history of changes on a field in a table.  This will
b=
e
> > the case in multiple tables.
> >
> > Can I create a trigger that loops the OLD and NEW values and compares
> > the values and if they are different creates a change string as
follows=
:
> >
> > e.g;
> >
> > FOR EACH field IN NEW
> >    IF field.value <> OLD.field.name THEN
> >       changes :=3D changes
> >            || field.name
> >            || ' was: '
> >            || OLD.field.value
> >            || ' now is: '
> >            || field.value
> >            || '\n\r';
> >    END IF
> > END FOR;
> >
> > Your help is really appreciated.
> >
> You can't in plpgsql.  It doesn't have the equivalent of a walkable
field=
s
> collection.  Its possible in some other procedure languages (I've seen
it
> done in C).
>
> Having said that, you might be able to create new and old temp tables
and
> then use the system tables to walk the columns list executing sql to
chec=
k
> for differences.
>
> something like
>
>  create temp table oldblah as select old.*;
>  create temp table newblah as select new.*;
>  for arecord in
>       select columnname
>       from pg_??columns??
>       join pg_??tables?? on ??columns??.xxx =3D ??tables??.yyy
>      where tablename =3D oldblah and pg_table_is_visible
>  loop
>
>       execute 'select old.' || arecord.columname || '::text , new. ' ||
> arecord.columname || '::text' ||
>                   ' from oldblah old, newblah new ' ||
>                   ' where oldblah.' || arecord.columnname || ' <>
> newblah.' ||arecord.columnname    into oldval,newval;
>
>      changes :=3D changes || arecord.columnname || ' was ' || oldval ||
'
> now ' || newval;
>  end loop;
>  execute 'drop table oldblah';
>  execute 'drop table newblah';
>
> performance could be awful though.
>
> klint.
>
> --
> Klint Gore
> Database Manager
> Sheep CRC
> A.G.B.U.
> University of New England
> Armidale NSW 2350
>
> Ph: 02 6773 3789  Fax: 02 6773 3266
> EMail: kgore4@[EMAIL PROTECTED]
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@[EMAIL PROTECTED]
)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>



--=20
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
kreno@[EMAIL PROTECTED]
 (928) 502-4240
..=B7:*=A8=A8*:=B7. .=B7:*=A8=A8*:=B7. .=B7:*=A8=A8*:=B7.

------=_Part_10618_27563894.1210122809277
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

This is easy with plpython.&nbsp; We do something
similar.<br><br>Kerri<br>=
<br><div class=3D"gmail_quote">On Tue, May 6, 2008 at 6:10 PM, Klint Gore
&=
lt;<a href=3D"mailto:kgore4@[EMAIL PROTECTED]
">kgore4@[EMAIL PROTECTED]
>&gt;
wrote:<br=
><blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid
rgb(204,=
 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class=3D"Wj3C7c">Fernando wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204,
=
204, 204); padding-left: 1ex;">
I want to keep a history of changes on a field in a table. &nbsp;This will
=
be the case in multiple tables.<br>
<br>
Can I create a trigger that loops the OLD and NEW values and compares the
v=
alues and if they are different creates a change string as follows:<br>
<br>
e.g;<br>
<br>
FOR EACH field IN NEW<br>
 &nbsp; &nbsp;IF field.value &lt;&gt; <a href=3D"http://OLD.field.name"
tar=
get=3D"_blank">OLD.field.name</a> THEN<br>
 &nbsp; &nbsp; &nbsp; changes :=3D changes<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| <a href=3D"http://field.name"
=
target=3D"_blank">field.name</a><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| &#39; was: &#39;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| OLD.field.value<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| &#39; now is: &#39;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| field.value<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|| &#39;\n\r&#39;;<br>
 &nbsp; &nbsp;END IF<br>
END FOR;<br>
<br>
Your help is really appreciated.<br>
</blockquote></div></div>
You can&#39;t in plpgsql. &nbsp;It doesn&#39;t have the equivalent of a
wal=
kable fields collection. &nbsp;Its possible in some other procedure
languag=
es (I&#39;ve seen it done in C).<br>
<br>
Having said that, you might be able to create new and old temp tables and
t=
hen use the system tables to walk the columns list executing sql to check
f=
or differences.<br>
<br>
something like<br>
<br>
 &nbsp;create temp table oldblah as select old.*;<br>
 &nbsp;create temp table newblah as select new.*;<br>
 &nbsp;for arecord in<br>
 &nbsp; &nbsp; &nbsp; select columnname<br>
 &nbsp; &nbsp; &nbsp; from pg_??columns??<br>
 &nbsp; &nbsp; &nbsp; join pg_??tables?? on ??columns??.xxx =3D
??tables??.=
yyy<br>
 &nbsp; &nbsp; &nbsp;where tablename =3D oldblah and
pg_table_is_visible<br=
>
 &nbsp;loop<br>
<br>
 &nbsp; &nbsp; &nbsp; execute &#39;select old.&#39; || arecord.columname
||=
 &#39;::text , new. &#39; || arecord.columname || &#39;::text&#39; ||<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39; from
=
oldblah old, newblah new &#39; ||<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;
where=
 oldblah.&#39; || arecord.columnname || &#39; &lt;&gt; newblah.&#39;
||arec=
ord.columnname &nbsp; &nbsp;into oldval,newval;<br>
<br>
 &nbsp; &nbsp; &nbsp;changes :=3D changes || arecord.columnname || &#39;
wa=
s &#39; || oldval || &#39; now &#39; || newval;<br>
 &nbsp;end loop;<br>
 &nbsp;execute &#39;drop table oldblah&#39;;<br>
 &nbsp;execute &#39;drop table newblah&#39;;<br>
<br>
performance could be awful though.<br>
<br>
klint.<br>
<br>
-- <br>
Klint Gore<br>
Database Manager<br>
Sheep CRC<br>
A.G.B.U.<br>
University of New England<br>
Armidale NSW 2350<br>
<br>
Ph: 02 6773 3789 &nbsp;Fax: 02 6773 3266<br>
EMail: <a href=3D"mailto:kgore4@[EMAIL PROTECTED]
"
target=3D"_blank">kgore4@[EMAIL PROTECTED]
><br><font color=3D"#888888">
<br>
<br>
-- <br>
Sent via pgsql-general mailing list (<a
href=3D"mailto:pgsql-general@[EMAIL PROTECTED]
" target=3D"_blank">pgsql-general@[EMAIL PROTECTED]
>)<br>
To make changes to your subscription:<br>
<a href=3D"http://www.postgresql.org/mailpref/pgsql-general"
target=3D"_bla=
nk">http://www.postgresql.org/mailpref/pgsql-general</a><br>
</font></blockquote></div><br><br clear=3D"all"><br>-- <br>Yuma
Educational=
 Computer Consortium<br>Compass Development Team<br>Kerri Reno<br><a href=
=3D"mailto:kreno@[EMAIL PROTECTED]
">kreno@[EMAIL PROTECTED]
> (928)
502-4240<br>.=B7:*=
=A8=A8*:=B7. .=B7:*=A8=A8*:=B7. .=B7:*=A8=A8*:=B7.

------=_Part_10618_27563894.1210122809277--
 




 8 Posts in Topic:
Is this possible in a trigger?
fernando@[EMAIL PROTECTED  2008-05-06 17:05:37 
Re: Is this possible in a trigger?
kgore4@[EMAIL PROTECTED]   2008-05-07 10:10:50 
Re: Is this possible in a trigger?
kreno@[EMAIL PROTECTED]   2008-05-06 19:13:29 
Re: Is this possible in a trigger?
xzilla@[EMAIL PROTECTED]   2008-05-07 01:12:25 
Re: Is this possible in a trigger?
fernando@[EMAIL PROTECTED  2008-05-07 09:37:21 
Re: Is this possible in a trigger?
kreno@[EMAIL PROTECTED]   2008-05-07 08:16:01 
Re: Is this possible in a trigger?
depesz@[EMAIL PROTECTED]   2008-05-07 13:01:35 
Re: Is this possible in a trigger?
valgog <valgog@[EMAIL   2008-05-07 04:04:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 11:53:42 CST 2008.