Re: [Postgresql-it] Problema teorico - multi progressivo

  • From: max jacob <max.jacob@xxxxxxxx>
  • To: postgresql-it@xxxxxxxxxxxxxxxxx
  • Date: Mon, 28 Feb 2005 12:55:42 +0100

qui c'era un errorino  select into v value from......

per il resto sembra ok


ok un corno! eccola (stavolta l'ho testata):

----------------------------------------------------------------------------------------------------

create or replace function contatore_next( text ) returns int as '

declare
  v int;

begin
  -- for update e necessario per lockare la riga
  select into v value from contatori where id = $1 for update;
  if not found then
     -- inserisce la riga
lock table contatori; -- cosi nessuno rischia di inserire un contatore con id uguale causando errore.
     insert into contatori ( id, value ) values ( $1, 1 );
     return 1;
  else
     -- incrementa e ritorna
     v:=v+1;
     update contatori set value = v where id = $1;
     return v;
  end if;

end;

' language plpgsql;

-----------------------------------------------------------------------------------------------
m.

Other related posts: