Re: sequence incrementing twice

Поиск
Список
Период
Сортировка
От ezra epstein
Тема Re: sequence incrementing twice
Дата
Msg-id EKucnfTI8I0-CpjdXTWc-w@speakeasy.net
обсуждение исходный текст
Ответы Re: sequence incrementing twice  ("Nigel J. Andrews" <nandrews@investsystems.co.uk>)
Список pgsql-general
"dan" <hassanbensober@lycos.com> wrote in message
news:9aa190bb.0401130958.6a3cfac5@posting.google.com...
> I have 2 tables, tab1 ( integer incremented sequence , col2, col3 )
> and tab2 ( integer from tab1, col4, col5 ).  When I call this function
> to add a record to each table:
>
> LOOP
>         select nextval('sequence') into id_car;  // for looping
>
>         INSERT INTO  tab1
>              VALUES (default, col2, col3);
>
>         INSERT INTO tab2
>              VALUES (currval('sequence'), col3, col4);
> END LOOP
>
> my sequence gets incremented twice.  If I use currval in the select,
> then it is not yet defined.  I'd love to have the sequence increment
> only once.

First off, you could instead do:

        INSERT INTO tab2
             VALUES (id_car, col3, col4);

Though that won't change the double-increment. For that you need to be sure
the sequence isn't being invoked somewhere else?  E.g., a trigger or a
default value or.... ???   (Or, of course, another session...)

== Ezra Epstein



В списке pgsql-general по дате отправления:

Предыдущее
От: "Bill McMilleon"
Дата:
Сообщение: Tool to ease development of plpgsql
Следующее
От: "Dean Arnold"
Дата:
Сообщение: Re: Tool to ease development of plpgsql