1

我有一个非常简单的函数定义如下:

CREATE OR REPLACE FUNCTION create_new_order(....) RETURNS integer AS
$BODY$
BEGIN
    PERFORM add_points_to_usage(client_id_p, date_in_p, total_points_p);

INSERT INTO orders (...) VALUES (...)
    RETURNING ident;
END;
$BODY$ LANGUAGE plpgsql;

我苦苦挣扎的地方是如何ident通过 RETURNING 子句实际返回存储在字段中的值。我尝试将值设置为一个变量,但这要么不起作用,要么我只是弄乱了语法。

4

1 回答 1

1

您缺少变量声明、INTO子句和 final RETURN

CREATE OR REPLACE FUNCTION create_new_order(....) RETURNS integer AS
$BODY$
DECLARE
  var_ident int;
BEGIN
    PERFORM add_points_to_usage(client_id_p, date_in_p, total_points_p);
    INSERT INTO orders (...) VALUES (...)
       RETURNING ident INTO var_ident;
    RETURN var_ident;
END;
$BODY$ LANGUAGE plpgsql;
于 2013-08-06T22:04:49.470 回答