在postgresql中创建一个函数,使用什么返回值

时间:2015-09-04 21:47:08

标签: sql postgresql function postgresql-9.3

我是PostgresSQL编程的新手,我正在尝试创建一个名为update_docket的函数,以便我可以从现有的存储过程中调用它。

在一些优秀的StackOverflow人员的帮助下,我能够按如下方式构建我的更新语句:

UPDATE incode_warrants iw
SET    warn_docket_no = iv.viol_docket_no
FROM   incode_warrantvs  iwvs
JOIN   incode_violations iv ON iv.viol_citation_no = iwvs.warnv_citation_no
                           AND iv.viol_viol_no = iwvs.warnv_viol_no
WHERE  iw.warn_rid = iwvs.warnv_rid;

在Navicat(My Postgresql界面)中,我在数据库上运行此查询,并收到带有计数的Affected Rows的返回消息。

现在我想接受这个查询并构建一个函数,但我不确定应该使用什么作为返回值。我猜测int4,我尝试使用返回值void构建它,但似乎没有任何效果。

有人能指出我如何创建此函数的正确方向,以便我可以在一个非常大的存储过程中调用它吗?

我已经查看了Postgresql手册,我确实有点困惑和恐惧,因为我不是一个SQL人员。

我相信函数我需要一个BEGIN和END来进行交易,我甚至试过return 1;但到目前为止我还没有成功保存这个功能。

谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢,但我不确定您运行的代码是什么。但是,如果您在postgres中执行通用存储过程,则可以执行以下操作:

CREATE OR REPLACE FUNCTION my_function([parameters to be used in stored procedure, if any])

RETURNS boolean AS -- [or you can have it return any data type such as integer]

$BODY$

DECLARE
    some_variable text;
    some_other_variable integer;
    another_variable timestamp;
BEGIN

/*
Put stored procedure here, in this case it would be your update statement
*/    

RETURN true; -- or you can return whatever you want based on what you declare above under RETURNS

END; -- closes the BEGIN clause

$BODY$ -- this is simply a delimiter to open and close the actual procedure that runs
  LANGUAGE plpgsql;

希望这能给你一些想法....但如果你发布了你想要运行的任何内容,那将会有所帮助。