postgresql - Can I use return value of INSERT...RETURNING in another INSERT? -
is possible?
insert table2 (val) values ((insert table1 (name) values ('a_title') returning id)); like using return value value insert row in second table reference first table?
you can starting postgres 9.1:
with rows ( insert table1 (name) values ('a_title') returning id ) insert table2 (val) select id rows in meanwhile, if you're interested in id, can trigger:
create function t1_ins_into_t2() returns trigger $$ begin insert table2 (val) values (new.id); return new; end; $$ language plpgsql; create trigger t1_ins_into_t2 after insert on table1 each row execute procedure t1_ins_into_t2();
Comments
Post a Comment