1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > mysql returning into_PostgreSQL在函数内返回returning

mysql returning into_PostgreSQL在函数内返回returning

时间:2019-01-22 20:34:46

相关推荐

mysql returning into_PostgreSQL在函数内返回returning

开发咨询,想在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; 参考: /Kenyon/blog/54376 /Kenyon/blog/108303

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。