Re: currval() within one statement

Поиск
Список
Период
Сортировка
От A. Kretschmer
Тема Re: currval() within one statement
Дата
Msg-id 20080122073637.GB21676@a-kretschmer.de
обсуждение исходный текст
Ответ на currval() within one statement  (silly_sad <sad@bankir.ru>)
Ответы Re: currval() within one statement  (sad <sad@bankir.ru>)
Список pgsql-sql
am  Tue, dem 22.01.2008, um 10:16:30 +0300 mailte silly_sad folgendes:
> Helo
> 
> is it expected that the currval() changes its value between calls within 
> one statement ?
> 
> Look the following call:
> 
> INSERT INTO ttt (a,b) SELECT currval('ttt_id_seq'), 'const' FROM ttt2;

This fails if you never call nextval() for this sequence within this
session.


> 
> Where the trigger before insert on ttt is defined and this trigger calls 
>  nextval('ttt_id_seq').

You don't need a TRIGGER. Just define your table with (a serial, ...)
and omit the column a if you INSERT a new row.



> I was surprised having different values of currval() in ttt.a

If you call nextval() befor the insert, then returns the currval(), for
instance, 5. If you call your insert with the TRIGGER like above, the
currval() returns this value 5, but your trigger fires and increase the
value.  And, maybe, an other process has increased the sequence also.

Conclusion, don't call nextval() within a TRIGGER, and insert either
nextval() for the column or omit this column.


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net


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

Предыдущее
От: silly_sad
Дата:
Сообщение: currval() within one statement
Следующее
От: sad
Дата:
Сообщение: Re: currval() within one statement