views:

18

answers:

1

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 varhcar(10)
);
+1  A: 

You need to use the INTO clause in the RETURNING to set the value being returned into your variable:

DECLARE myid OAMENI.id%TYPE;

INSERT INTO oameni 
VALUES 
  (default,'lol') 
RETURNING id INTO myid;

You also need to specify the data type of your variable; I'm glad to see postgresql supports %TYPE and %ROWTYPE.

OMG Ponies
i still get a syntax error, is this available only inside a plpgsql function, or is available in normal posgresql also ?
Omu
@Omu: How are you attempting to run this? And what version of PostgreSQL?
OMG Ponies
I have the latest version 9 RC1, and I just pasted your code inside the sql editor
Omu
yes it's a id serial primary key
Omu
You are confusing procedural code w/ SQL. Declaring variables, cursors, loops etc are all procedural and have to be in a function or in 9.0 a DO anonymous block.
Scott Bailey
@Scott Bailey: that's the only use I could see for this, to be using within a function/stored procedure.
OMG Ponies
@OMG Ponies: Sorry, I was talking to Omu who was trying to run individual plpgsql statements in a query window.
Scott Bailey