PostgreSQL存储过程(函数)的正确语法是什么? [英] What is the proper syntax for PostgreSQL stored procedures (functions)?

查看:617
本文介绍了PostgreSQL存储过程(函数)的正确语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PostgreSQL中编写两种类型的存储过程.据我了解,Postgre仅具有功能.我想知道是否有人可以看一下我的代码并提供指针.另外,我对命令的空格/换行符不熟悉.

I'm trying to write two types of stored procedures in PostgreSQL. From what I understand Postgre only has functions. I was wondering if someone can take a look at my code and offer pointers. Also, I'm am not familiar whether with the spacing/new lines of commands.

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

The first function needs to take input from user and add it onto a table. Suppose we have a table name "Car" with attributes "model" and "year". Will this be a correct stored function to add a new car to the table?

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)

----------进行中代码 功能1

---------- Work in Progress code Function 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;

现在可以使用了! (将价值模型和年份插入汽车"中.

This now works! (inserts values model and year into Car).

推荐答案

来自您将在那里找到答案,也许可以从中学到两到三个有用的东西.

You will find your answer there and, maybe, learn two or three useful things on the process.

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

You might be particularly interested in the RETURNS TABLE construct.

这篇关于PostgreSQL存储过程(函数)的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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