UPDATE... FROM - will ORDER BY not respected?

Поиск
Список
Период
Сортировка
От Carlo Stonebanks
Тема UPDATE... FROM - will ORDER BY not respected?
Дата
Msg-id gt7ffg$1nif$1@news.hub.org
обсуждение исходный текст
Ответы Re: UPDATE... FROM - will ORDER BY not respected?  (Adam Rich <adam.r@sbcglobal.net>)
Список pgsql-general
(FOR PG VERSION 8.3.6 running on Windows 2003 Server x64)

We have a function that assigns unique ID's (to use as row identifiers) to a
table via an UPDATE using nextval(). This table is imported from another
source, and there is a "sequencing" field to let the query know in which
order to assign the row identifiers. (Please do not confuse the sequencing
field with a sequence value from nextval())

The UPDATE command gets the order of the rows to update using a FROM clause,
which in turn reads from a sub-query to get the rows in the order of "seq".

The problem is that the UPDATE is NOT behaving as if it is receiving the
sequence identifiers in the order specified. In fact, it appears it is
returned in REVERSE order (assigning id's in reverse order based on the
values in seq)

Here is the essence of the query (further below you will find the full DDL
code of the function).

UPDATE impt_table
SET id = nextval(''id_seq'')
FROM
          (SELECT seq
          FROM impt_table
          WHERE id IS NULL
          ORDER BY seq
          ) AS empty_ids
WHERE
          impt_table.seq = empty_ids.seq
          AND impt_table.id IS NULL;

Was I wrong in assuming that the UPDATE would respect the order of rows
coming out of the sub-clause? Is there a better way to do this?

Thanks, Carlo


DDL CODE FOR FUNCTION
CREATE OR REPLACE FUNCTION "mdx_import"."impt_id_seed_from_impt_seq" (text)
RETURNS boolean AS
$body$
/* New function body */
declare
   cmd varchar;
begin
   cmd =
      'update mdx_import.'||$1||'
       set impt_id = nextval(''mdx_import.impt_id_seq'')
       from
          (select impt_seq
          from mdx_import.'||$1||'
          where impt_id is null
          order by impt_seq
          ) as empty_impt_ids
       where
          '||$1||'.impt_seq = empty_impt_ids.impt_seq
          and '||$1||'.impt_id is null;';
   execute cmd;
   return true;
end;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;


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

Предыдущее
От: Allan Kamau
Дата:
Сообщение: Re: Restore Crashes Postgres
Следующее
От: Christine Penner
Дата:
Сообщение: Re: Restore Crashes Postgres