Thread: Inc

Inc

From
"Ezequias Rodrigues da Rocha"
Date:
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/


Re: Inc

From
Richard Huxton
Date:
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


Re: Inc

From
"Ezequias Rodrigues da Rocha"
Date:
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/


Re: Inc

From
Richard Broersma Jr
Date:
--- 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.



Re: Inc

From
Andreas Kretschmer
Date:
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°


Re: Inc

From
Andreas Kretschmer
Date:
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°


Re: Inc

From
"Ezequias Rodrigues da Rocha"
Date:
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
>


Re: Inc

From
Richard Huxton
Date:
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


Re: Inc

From
"Ezequias Rodrigues da Rocha"
Date:
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
>