PostgreSQL存储过程(函数)的正确语法是什么?

tvg*_*234 5 sql postgresql stored-procedures plpgsql

我正在尝试在PostgreSQL中编写两种类型的存储过程.从我的理解Postgre只有功能.我想知道是否有人可以查看我的代码并提供指针.另外,我不熟悉是否使用了间距/新的命令行.

第一个函数需要从用户获取输入并将其添加到表中. 假设我们有一个表名为"Car",其属性为"model"和"year".这是一个正确的存储功能,可以将新车添加到桌面吗?

CREATE OR REPLACE FUNCTION
    addto_car(model IN Car.model%type, year IN Car.year%type)
RETURNS
    void
AS $$
BEGIN
    INSERT INTO Car VALUES(model, year);
END;
$$ LANGUAGE plpgsql; (#Is this correct? I'm using postgresql 9)
Run Code Online (Sandbox Code Playgroud)

----------正在进行中的代码 功能1

CREATE OR REPLACE FUNCTION
    addto_car(In model Car.model%type, IN year Car.year%type)
AS $$
BEGIN
    INSERT INTO Car VALUES(model, year);
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

这现在有效!(将值模型和年份插入Car).

Adr*_*iro 5

来自官方文档

CREATE [ OR REPLACE ] FUNCTION
    name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
    [ RETURNS rettype
      | RETURNS TABLE ( column_name column_type [, ...] ) ]
  { LANGUAGE lang_name
    | WINDOW
    | IMMUTABLE | STABLE | VOLATILE
    | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
    | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
    | COST execution_cost
    | ROWS result_rows
    | SET configuration_parameter { TO value | = value | FROM CURRENT }
    | AS 'definition'
    | AS 'obj_file', 'link_symbol'
  } ...
    [ WITH ( attribute [, ...] ) ]
Run Code Online (Sandbox Code Playgroud)

你会在那里找到你的答案,也许,在这个过程中学习两三个有用的东西.

您可能对RETURNS TABLE构造特别感兴趣.