Good day, there is such a code

CREATE OR REPLACE FUNCTION SubmitReview() RETURNS VOID AS $$ declare a INT; begin a := 1; while a <= (select max(id_) from test_f) loop insert into consumer (id_, name_, type_, abonentid_) values ((select max(id_)+1 from consumer), (select name_ from test_f where id_ = a), (select type_ from test_f where id_ = a), (select abonentid_ from test_f where id_ = a) ); a = a + 1; end loop; end $$ LANGUAGE plpgsql; 

those. The consumer table must be filled with fields from the TEST_F TABLE, for this there is a counter a. cycle compiles but nothing happens

  • one
    Why cycle? It can simply insert into consumer (name_, type_, abonentid_) select name_,type_,abonentid_ from test_f only need to have the id itself assigned by normal means, max (id) +1 it is very crooked. - Mike
  • the problem is that the id_ column has a bigint not null format, and it is not possible to make it a serial format, therefore, so clumsily and through the cycle - Wit
  • There is a bigserial data type. Although it is too late to convert now. Or you can use sequence. As for this insertion, you can do something like insert into consumer (id_,name_, type_, abonentid_) select maxId+row_number() over(order by id_),name_,type_,abonentid_ from test_f, (select max(id_) maxId from consumer) M - Mike
  • I thank, but I still do not understand why such manual increment is bad, when 1 is added to the maximum id by the hands, the uniqueness is preserved - Wit
  • And you imagine that transaction 1 will take max () + 1 insert a record, but the commit has not yet passed and at that time transaction 2 will receive max () + 1 from another connection, but it still does not see the record that was inserted first transaction, since commit has not yet passed and therefore will receive the same id as the first - Mike

0