gpt4 book ai didi

sql - 创建一个不返回任何内容的用户定义函数

转载 作者:行者123 更新时间:2023-11-29 12:47:19 25 4
gpt4 key购买 nike

我正在尝试创建一个不返回任何内容的用户定义函数。我有 body :select form from bookings where id=$1 其中 $1 是函数采用的第一个也是唯一一个参数。

我试过了

create function findBookgins(character varying(100)) returns void
select form from bookings where id=$1;

我在选择时遇到错误。我只想显示创建的表。

最佳答案

函数调用应该类似于:

SELECT * FROM findBookgins('FooBar' :: character varying);

或者

SELECT findBookings('Foo');

或者

Perform findBookings('Bar'); 
/* note this call will not return anything but
is only available when calling function from other functions */

编辑:好,让我们一步步来看问题。首先什么都不返回: Here's a perfectly good explanation about how function return works in postgresql

接下来如何定义函数(因为你的代码是纯 sql,我猜你也需要 sql 中的函数):

CREATE OR REPLACE FUNCTION findBookgins(prm_booking character varying(100))
RETURNS SETOF bookings AS
'SELECT * FROM bookings WHERE id = $1';
LANGUAGE sql VOLATILE
ROWS 1000;

此函数应返回符合您给定条件的所有预订:

SELECT * FROM findBookings('15');

将返回所有 ID 为 15 的预订。

我假设这就是你想要做的,否则你的函数将不会做任何事情,要使用循环你需要 plpgsql 函数

更多关于 plpgsql 过程语言语法的信息 here

关于sql - 创建一个不返回任何内容的用户定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13025091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com