将整个选择语句作为参数传递-PostgreSQL函数

时间:2020-07-08 07:28:10

标签: postgresql stored-procedures

我将整个select语句作为参数传递给Postgres函数,如下所示...

select * from demo_spnsearch('select* from public.tbl_spnsearch where spnid=78 and spnyear=2000::varchar and region=Australia')

我遇到错误

SQL错误[42703]:错误:“澳大利亚”列不存在其中: PL / pgSQL函数demo_spnsearch(text)在RETURN QUERY的第6行

但是当我通过没有区域的相同

select * from demo_spnsearch('select* from public.tbl_spnsearch where spnid=78 and spnyear=2000::varchar ')

它工作正常。

请帮助...

1 个答案:

答案 0 :(得分:0)

您必须将region值放在引号中。您可以使用以下任一选项

SELECT *
FROM demo_spnsearch('select* from public.tbl_spnsearch where spnid=78 and spnyear=2000::varchar and region=''Australia''') -- note the quotes here region=''Australia''

使用Postgres内置的format()函数。

SELECT *
FROM  demo_spnsearch(format('select* from public.tbl_spnsearch where spnid=%s and spnyear=%s::varchar and region=%L', 78, 2000, 'Australia'))