PostgreSQL 使用RETURNING返回值
发布时间:2020-12-13 17:19:51 所属栏目:百科 来源:网络整理
导读:用法: 在INSERT INTO或者UPDATE的时候在最后面加上RETURNING colname,PostgreSQL会在插入或者更新数据之后会返回你指定的字段。 postgres=# d tb3 Table "public.tb3" Column | Type | Modifiers --------+-------------------+-------------------------
用法: postgres=# d tb3
Table "public.tb3"
Column | Type | Modifiers --------+-------------------+----------------------------------------------
id | integer | not null default nextval('tb3_id_seq'::regclass)
name | character varying |
postgres=# insert into tb3(name) values('aa')returning name;
name ------
aa
(1 row)
INSERT 0 1
postgres=# insert into tb3(name) values('aa')returning id;
id ----
2
(1 row)
INSERT 0 1
postgres=# insert into tb3(name) values('aa')returning id,name;
id | name ----+------
3 | aa
(1 row)
INSERT 0 1
postgres=# update tb3 tb3 set name='bb' where id=1 returning name;
name ------
bb
(1 row)
UPDATE 1
postgres=# do language plpgsql $$ postgres$# declare postgres$# n character varying;
postgres$# begin postgres$# update tb3 set name='ss' where id=3 returning name into n;
postgres$# raise notice 'n is %',n;
postgres$# insert into tb3(name) values(n);
postgres$# end;
postgres$# $$;
NOTICE: n is ss
DO postgres=# select * from tb3;
id | name
----+------
2 | aa
1 | bb
3 | ss
4 | ss
(4 rows)
postgres=#
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |