Обсуждение: Inserting a record data type into a table

Поиск
Список
Период
Сортировка

Inserting a record data type into a table

От
Graeme Hinchliffe
Дата:
Hiya
    Is it possible to insert a record data type into a table, or am I going
to have to refer to each attribute individually in an insert?

what I would like to be able to do is simply

INSERT INTO newtable VALUES (NEW);

and have the whole NEW record in a trigger inserted into the new table.

Thanks

--
-----
Graeme Hinchliffe (BSc)
Core Systems Designer
Zen Internet (http://www.zen.co.uk/)

Direct: 0845 058 9074
Main  : 0845 058 9000
Fax   : 0845 058 9005

Вложения

Re: Inserting a record data type into a table

От
Tom Lane
Дата:
Graeme Hinchliffe <graeme.hinchliffe@zeninternet.co.uk> writes:
> what I would like to be able to do is simply
> INSERT INTO newtable VALUES (NEW);

In 8.0 it works to spell it "new.*".

regression=# create table z1 (f1 int, f2 text);
CREATE TABLE
regression=# create table z2 (f1 int, f2 text);
CREATE TABLE
regression=# create function z1t() returns trigger as $$
regression$# begin
regression$#   insert into z2 values(new.*);
regression$#   return new;
regression$# end$$ language plpgsql;
CREATE FUNCTION
regression=# create trigger z1t  before insert on z1 for each row
regression-# execute procedure z1t();
CREATE TRIGGER
regression=# insert into z1 values (42, 'foo');
INSERT 0 1
regression=# select * from z2;
 f1 | f2
----+-----
 42 | foo
(1 row)

regression=#

            regards, tom lane