Re: Rule problem with OLD / NEW record set (repost)

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Rule problem with OLD / NEW record set (repost)
Дата
Msg-id 28546.1106956984@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Rule problem with OLD / NEW record set (repost)  (Ralph Graulich <maillist@shauny.de>)
Ответы Re: Rule problem with OLD / NEW record set (repost)  (Ralph Graulich <maillist@shauny.de>)
Список pgsql-general
Ralph Graulich <maillist@shauny.de> writes:
> CREATE VIEW view_table1 AS SELECT * FROM table1;

> -- create a rule for update
> CREATE OR REPLACE RULE ru_view_table1_update
> AS
> ON UPDATE TO view_table1 DO INSTEAD
>    (
>    -- insert a new record with the old id, old version number incremented
>    -- by one, versionflag set to 'Y' and the new content
>    INSERT INTO table1 (id, version, vnoflag, content) VALUES (OLD.id,
> OLD.version+1, 'Y', NEW.content);
>    -- update the old version and set its versionflag to 'N' as it is no
>    -- longer the current record
>    UPDATE table1 SET vnoflag = 'N' WHERE id = OLD.id AND version = OLD.version;
>    );

> It seems like the UPDATE statement updates both the old and the new
> version.

Yes, because (loosely speaking) OLD refers to the view, and once you've
done the INSERT there is now another matching row in the view.  Try
doing the UPDATE first, then the INSERT.

            regards, tom lane

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

Предыдущее
От: Stephan Szabo
Дата:
Сообщение: Re: rowset-returning function mismatch
Следующее
От: Tom Lane
Дата:
Сообщение: Re: rowset-returning function mismatch