加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL在函数内返回returning

发布时间:2020-12-13 17:31:54 所属栏目:百科 来源:网络整理
导读:开发咨询,想在function返回之前insert或者update的值,postgresql有个returning的特性,可以返回之前各种DML(insert,delete,update)掉的值,比mysql的返回last_insert_id更丰富一点。 环境: PostgreSQL 9.3.2 CentOS 6.3 类似的Function如下: CREATE OR RE
开发咨询,想在function返回之前insert或者update的值,postgresql有个returning的特性,可以返回之前各种DML(insert,delete,update)掉的值,比mysql的返回last_insert_id更丰富一点。

环境:
PostgreSQL 9.3.2
CentOS 6.3

类似的Function如下:
CREATE OR REPLACE FUNCTION fun_test(v_id int,v_remark text)
  RETURNS integer AS
$BODY$

declare 
   o_id int;
begin

   perform 1  from foo where id=v_id and remark = v_remark ;

if not found then 
   insert into foo(remark) values (v_remark) returning id into o_id;
return  o_id;
else 
   update foo set  id=v_id,remark=v_remark where id=v_id and remark = v_remark  returning id into o_id;
return o_id ;
end if;

exception when others then
raise exception 'exec fun fun_test error,PLZ contact DBA!';
return  999;

end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION fun_test(int,text)
  OWNER TO postgres;
参考: http://my.oschina.net/Kenyon/blog/54376 http://my.oschina.net/Kenyon/blog/108303

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读