postgresql - 将整个 select 语句作为参数传递 - PostgreSQL 函数
问题描述
我将整个 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) 第 6 行,位于 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'))
推荐阅读
- c# - 我如何才能在 C# 中只获取异常堆栈跟踪消息的第一行?
- javascript - ReactJS:超出最大部门/私人路线
- javascript - 如何从两个不同的中继连接中删除共享节点?
- android - Lottie for Xamarin Android 无法正常工作其重叠并在 Xamarin.Android 中产生未接受的动画
- powerbi - 如何在 Power BI 中用 3 列展平数据?
- amazon-dynamodb - 有什么方法可以在 DynamoDB 中初始化一个数字列表的增量?
- python - WinError 10061:无法建立连接,因为目标机器主动拒绝 - 聊天室程序
- python - 两个相同的 Python 文件在运行时表现不同的可能原因是什么?
- api - 是否有任何服务可以提供 REST API 数据以利用更好的索引?
- javascript - 如何使用 @solana/web3.js 从 Solana 中的自定义令牌中删除铸币权限?