Variable value assignment using RETURNING clause

Viewed 54341

I try to do this, but it's a syntax error, what am I doing wrong?

declare myid := insert into oameni values(default,'lol') returning id;

my table:

create table oameni
(
 id   serial primary key,
 name varchar(10)
);
2 Answers

Adding to the main answer, it's worth noting that if you are doing this outside a stored procedure, you must wrap the code in a "DO" block, like so:

DO $$ 
DECLARE
    myid mytable.id%TYPE;
BEGIN
    INSERT INTO mytable (...) 
      VALUES (...)
      RETURNING id INTO myid;
END $$
Related