Unix Technical Forum

Inc

This is a discussion on Inc within the pgsql Sql forums, part of the PostgreSQL category; --> Hi list, There is any function to increment with 1 some field. For example. I have a table with ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 03:10 PM
Ezequias Rodrigues da Rocha
 
Posts: n/a
Default Inc

Hi list,

There is any function to increment with 1 some field.

For example. I have a table with a field that on each update it
incrementes a field that is allways configured to 0 before the
starting of updates.

Regards

--
Ezequias Rodrigues da Rocha
http://ezequiasrocha.blogspot.com/
use Mozilla Firefox:http://br.mozdev.org/firefox/

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 03:10 PM
Richard Huxton
 
Posts: n/a
Default Re: Inc

Ezequias Rodrigues da Rocha wrote:
> Hi list,
>
> There is any function to increment with 1 some field.
>
> For example. I have a table with a field that on each update it
> incrementes a field that is allways configured to 0 before the
> starting of updates.


Something like:

CREATE FUNCTION my_autoinc() RETURNS TRIGGER AS $$
BEGIN
NEW.counter := NEW.counter + 1;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER my_counter_trig BEFORE UPDATE ON my_table
FOR EACH ROW EXECUTE PROCEDURE my_autoinc();

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 03:10 PM
Ezequias Rodrigues da Rocha
 
Posts: n/a
Default Re: Inc

Thank you but I must inc an specific row. How to do that ?

Ezequias

2007/3/6, Richard Huxton <dev@archonet.com>:
> Ezequias Rodrigues da Rocha wrote:
> > Hi list,
> >
> > There is any function to increment with 1 some field.
> >
> > For example. I have a table with a field that on each update it
> > incrementes a field that is allways configured to 0 before the
> > starting of updates.

>
> Something like:
>
> CREATE FUNCTION my_autoinc() RETURNS TRIGGER AS $$
> BEGIN
> NEW.counter := NEW.counter + 1;
> RETURN NEW;
> END;
> $$ LANGUAGE plpgsql;
>
> CREATE TRIGGER my_counter_trig BEFORE UPDATE ON my_table
> FOR EACH ROW EXECUTE PROCEDURE my_autoinc();
>
> --
> Richard Huxton
> Archonet Ltd
>



--
Ezequias Rodrigues da Rocha
http://ezequiasrocha.blogspot.com/
use Mozilla Firefox:http://br.mozdev.org/firefox/

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 03:10 PM
Richard Broersma Jr
 
Posts: n/a
Default Re: Inc

--- Ezequias Rodrigues da Rocha <ezequias.rocha@gmail.com> wrote:

> Thank you but I must inc an specific row. How to do that ?
>


Does,

UPDATE your_table
SET your_row = your_row +1
WHERE your_pkey = <some value>;

not do what you need?

Regards,

Richard Broersma Jr.


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-19-2008, 03:10 PM
Andreas Kretschmer
 
Posts: n/a
Default Re: Inc

Ezequias Rodrigues da Rocha <ezequias.rocha@gmail.com> schrieb:

> Thank you but I must inc an specific row. How to do that ?


Read the answer again.

Please, no silly TOFU (german synonym for text above, fullquote below).

> >> For example. I have a table with a field that on each update it
> >> incrementes a field that is allways configured to 0 before the
> >> starting of updates.

> >Something like:
> >CREATE FUNCTION my_autoinc() RETURNS TRIGGER AS $$
> >BEGIN
> > NEW.counter := NEW.counter + 1;
> > RETURN NEW;
> >END;
> >$$ LANGUAGE plpgsql;
> >CREATE TRIGGER my_counter_trig BEFORE UPDATE ON my_table
> >FOR EACH ROW EXECUTE PROCEDURE my_autoinc();


Your task is only to adapt this to your table-design.


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknow)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-19-2008, 03:10 PM
Andreas Kretschmer
 
Posts: n/a
Default Re: Inc

Richard Broersma Jr <rabroersma@yahoo.com> schrieb:

> --- Ezequias Rodrigues da Rocha <ezequias.rocha@gmail.com> wrote:
>
> > Thank you but I must inc an specific row. How to do that ?
> >

>
> Does,
>
> UPDATE your_table
> SET your_row = your_row +1
> WHERE your_pkey = <some value>;
>
> not do what you need?


I think, he is searching for a TRIGGER and he has got the solution.


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknow)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-19-2008, 03:10 PM
Ezequias Rodrigues da Rocha
 
Posts: n/a
Default Re: Inc

Now that's ok. Thank you all. I just thought that there was a function
to inc an integer field in postgresql.

Best regards
Ezequias

2007/3/6, Andreas Kretschmer <akretschmer@spamfence.net>:
> Richard Broersma Jr <rabroersma@yahoo.com> schrieb:
>
> > --- Ezequias Rodrigues da Rocha <ezequias.rocha@gmail.com> wrote:
> >
> > > Thank you but I must inc an specific row. How to do that ?
> > >

> >
> > Does,
> >
> > UPDATE your_table
> > SET your_row = your_row +1
> > WHERE your_pkey = <some value>;
> >
> > not do what you need?

>
> I think, he is searching for a TRIGGER and he has got the solution.
>
>
> Andreas
> --
> Really, I'm not out to destroy Microsoft. That will just be a completely
> unintentional side effect. (Linus Torvalds)
> "If I was god, I would recompile penguin with --enable-fly." (unknow)
> Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-19-2008, 03:10 PM
Richard Huxton
 
Posts: n/a
Default Re: Inc

Ezequias Rodrigues da Rocha wrote:
> Thank you but I must inc an specific row. How to do that ?


I'm not sure I understand. A row in a different table?

....
BEGIN
UPDATE some_table SET counter = counter + 1;
END;
....

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-19-2008, 03:10 PM
Ezequias Rodrigues da Rocha
 
Posts: n/a
Default Re: Inc

I did like this:

update base.destinationTable set MyCountField = MyCountField + 1 where
id = new.keyField_ID;

I think it works becouse my tests are ok.

My really thank you all.

Ezequias
2007/3/6, Richard Huxton <dev@archonet.com>:
> Ezequias Rodrigues da Rocha wrote:
> > Thank you but I must inc an specific row. How to do that ?

>
> I'm not sure I understand. A row in a different table?
>
> ...
> BEGIN
> UPDATE some_table SET counter = counter + 1;
> END;
> ...
>
> --
> Richard Huxton
> Archonet Ltd
>


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:34 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com