1

Using SQL Server I can write the following statement...

create table Person(
    id int identity(1,1),
    name varchar(50)
)

insert into Person(name) values ('John')
select * from Person where id = scope_identity()

In Postgres I can do this:

CREATE TABLE public.Person
(
  id serial primary key,
  name character varying(10) NOT NULL
)

INSERT INTO Person(name) VALUES ('Smith', 'John') RETURNING id;

How would I write an equivalent statement like I did in the SQL example where don't return the id, but the entire row that was just inserted?

John Livermore
  • 30,235
  • 44
  • 126
  • 216

2 Answers2

2

As @cur4so stated, or alternatively

INSERT INTO Person(name) VALUES ('Smith', 'John') RETURNING *;
Ezequiel Tolnay
  • 4,302
  • 1
  • 19
  • 28
1
INSERT INTO Person (name) VALUES ('Smith John'); 
select * from Person where id = currval('person_id_seq'); 
cur4so
  • 1,750
  • 4
  • 20
  • 28
  • 1
    `lastval()` is another alternative. That way you don't need to know the sequence name –  May 11 '16 at 06:03