带有可选参数的Postgresql函数

问题描述 投票:0回答:1

有没有一种方法可以创建一个可以用可变数量的参数(逗号分隔,因此位置)调用的函数。例如,用function1(param1,param2)调用这样的函数,并可能用function1(,param2)或function1(param1,)调用它?我用默认参数创建了一个函数,但调用它时出错:

select * from iDxi('3 days',) order by "Date" asc
ERROR:  syntax error at or near ")"
LINE 1: select * from iDxi('3 days',) order by "Date" asc

我的函数定义是:

CREATE OR REPLACE FUNCTION public.idxi(
    mydated text DEFAULT '99 year'::text,
    mydatef text DEFAULT '-99 year'::text)
RETURNS TABLE...

[不提供参数select * from idxi()时有效,但仅提供一个...时不起作用...

我在哪里错了?

postgresql parameters stored-functions
1个回答
0
投票

如果只想传递第二个参数,请按名称传递:

select *
from idxi(mydatef => '-3 days');

如果只想传递第一个参数,则只需按位置传递(在参数后没有,

select *
from idxi('3 days'); 

也可以是名称:

select *
from idxi(mydated => '3 days');

无关,但:

如果要向函数传递间隔,则应声明该类型的参数:

CREATE OR REPLACE FUNCTION public.idxi(
    mydated interval DEFAULT '99 year'::interval,
    mydatef interval DEFAULT '-99 year'::interval)
RETURNS TABLE...
© www.soinside.com 2019 - 2024. All rights reserved.