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

Passing entire select statement as a parameter - PostgreSQL function

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

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

我收到一个错误

SQL Error [42703]: ERROR: column "australia" does not exist Where: PL/pgSQL function demo_spnsearch(text) line 6 at RETURN QUERY

但是当我通过相同的区域时

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

它工作正常。

请帮忙...

您必须将 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'))