Re: how to create a sequence in a stored proc?

Поиск
Список
Период
Сортировка
От Raymond O'Donnell
Тема Re: how to create a sequence in a stored proc?
Дата
Msg-id 4F9AD1F5.6000401@iol.ie
обсуждение исходный текст
Ответ на Re: how to create a sequence in a stored proc?  ("J.V." <jvsrvcs@gmail.com>)
Список pgsql-general
On 27/04/2012 17:35, J.V. wrote:
> Right, I understand that fully, and have used SQL inside a stored proc
> before, but in this case as I mentioned, I need to first do a select
> from a table to get a max value, store that in a variable and then use
> that variable in a create sequence sql statement.

Something like this, off the top of my head and untested:

create or replace function make_sequence_for_table()
returns void
as
$$
declare
  max_value integer;
begin
  select max(my_column) into max_value from my_table;

   create sequence my_sequence
    start (max_value + 1)
    owned by my_table.my_column;

  alter table my_table
    alter column my_column
    set default nextval('my_sequence');

  return;
end;
$$
language plpgsql;


For extra marks, pass the table name in as a parameter, construct the
SQL as a string and execute it using EXECUTE (the pl/pgsql version, not
the command for executing prepared statements).

Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

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

Предыдущее
От: Merlin Moncure
Дата:
Сообщение: Re: how to create a sequence in a stored proc?
Следующее
От: Steve Atkins
Дата:
Сообщение: Re: how to create a sequence in a stored proc?