从带有OUT参数的函数返回 [英] Returning from a function with OUT parameter

查看:89
本文介绍了从带有OUT参数的函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误,但我不知道问题出在哪里.

I have an error, but I don't know what the problem is.

我想执行一个函数并从默认情况下填充的列中返回一个值,该序列是序列-等效于currval(sequence).

I want execute a function and return a value from a column filled in by the column default, a sequence - the equivalent of currval(sequence).

我使用:
PostgreSQL 9.0
pgAdmin III

I use:
PostgreSQL 9.0
pgAdmin III

CREATE OR REPLACE FUNCTION name_function(in param_1 character varying
                                      , out param_2 bigint)
  AS
$$
BEGIN
    INSERT INTO table (collumn_seq,param_1) VALUES (DEFAULT,param_1)
    returning collumn_seq;
--where:collumn_seq reference a collumn serial..
END;
$$
  LANGUAGE plpgsql VOLATILE;

我可以创建没有错误的函数,但是在尝试执行时,会返回以下错误:

I can create the function without error but when trying to execute, the following error is returned:

SELECT name_function('GHGHGH');

ERROR: The query has no destination for result data

推荐答案

它会像这样工作:

CREATE OR REPLACE FUNCTION name_function(param_1 character varying
                                   , OUT param_2 bigint) AS
$func$
BEGIN
    INSERT INTO table (collumn_seq, param_1)
    VALUES (DEFAULT, param_1)
    RETURNING collumn_seq
    INTO param2;
END
$func$ LANGUAGE plpgsql;

通常,您将添加RETURN语句,但是具有OUT参数,这是可选的.
有关更多详细信息,请参阅手册:

Normally, you would add a RETURN statement, but with OUT parameters, this is optional.
Refer to the manual for more details:

  • Returning from a function
  • Executing a Query with a Single-row Result

这篇关于从带有OUT参数的函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆