postgresql 创建一个不返回任何内容的用户定义函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13025091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a user defined function that doesn't return anything
提问by Celeritas
I am trying to make a user defined function that returns nothing. I have the body:
select form from bookings where id=$1
where $1 is the first and only argument the function takes.
我正在尝试制作一个不返回任何内容的用户定义函数。我有正文:
select form from bookings where id=$1
其中 $1 是函数采用的第一个也是唯一的参数。
I've tried
我试过了
create function findBookgins(character varying(100)) returns void
select form from bookings where id=;
and I get an error at the select. I just want the table created to be displayed.
我在选择时出错。我只想显示创建的表。
回答by ertx
Function call should look something like:
函数调用应该类似于:
SELECT * FROM findBookgins('FooBar' :: character varying);
Or
或者
SELECT findBookings('Foo');
Or
或者
Perform findBookings('Bar');
/* note this call will not return anything but
is only available when calling function from other functions */
Edit: Okay let's look at the problem step by step. First of all returning nothing: Here's a perfectly good explanation about how function return works in postgresql
编辑:好的,让我们一步一步地看问题。首先什么都不返回: 这是关于函数返回如何在 postgresql 中工作的完美解释
Next how to define function (since your code is pure sql
, i'm guessing you need a function in sql
aswell):
接下来如何定义函数(因为你的代码是纯代码sql
,我猜你还需要一个函数sql
):
CREATE OR REPLACE FUNCTION findBookgins(prm_booking character varying(100))
RETURNS SETOF bookings AS
'SELECT * FROM bookings WHERE id = ';
LANGUAGE sql VOLATILE
ROWS 1000;
This function should return all the bookings that match your given criteria as so:
此函数应返回符合您给定条件的所有预订,如下所示:
SELECT * FROM findBookings('15');
will return all bookings with id 15.
将返回 ID 为 15 的所有预订。
I'm assuming that's what you're trying to do because otherwise your function wouldn't do anything, to use loops you need plpgsql
functions
我假设这就是你想要做的,因为否则你的函数不会做任何事情,要使用循环你需要plpgsql
函数
More about plpgsql procedural language syntax here
有关 plpgsql 过程语言语法的更多信息,请参见此处
回答by Sam Choukri
You have syntax errors in your function. You need to properly quote the body of the function.
您的函数中有语法错误。您需要正确引用函数体。
CREATE FUNCTION findBookgins(character varying(100)) RETURNS VOID AS
$$
SELECT form FROM bookings WHERE id=;
$$
LANGUAGE SQL
;
I think the documentation on SQL-based functionswill be particularly helpful to you.
我认为有关基于 SQL 的函数的文档对您特别有帮助。
By the way, I really can't believe you want this particular function to return nothing. What is the point of that?
顺便说一句,我真的不敢相信你希望这个特定的函数什么都不返回。那有什么意义呢?