使用SQL Server我可以编写以下语句......
create table Person(
id int identity(1,1),
name varchar(50)
)
insert into Person(name) values ('John')
select * from Person where id = scope_identity()
在Postgres中,我可以这样做:
CREATE TABLE public.Person
(
id serial primary key,
name character varying(10) NOT NULL
)
INSERT INTO Person(name) VALUES ('Smith', 'John') RETURNING id;
我如何编写一个等效语句,就像我在SQL示例中所做的那样,不返回id,而是刚刚插入的整行?
答案 0 :(得分:2)
正如@ cur4so所述,或者
INSERT INTO Person(name) VALUES ('Smith', 'John') RETURNING *;
答案 1 :(得分:1)
INSERT INTO Person (name) VALUES ('Smith John');
select * from Person where id = currval('person_id_seq');