【发布时间】:2020-09-26 14:00:47
【问题描述】:
PostgreSQL 11.4,由 Visual C++ build 1914 编译,64 位
在 Stackoverflow 中审阅了几十篇文章,没有真正的匹配。需要:传递一个逗号分隔的字符串(id 值),并将该列表与“ANY”postgresql 子句一起使用。
代码
return query execute
'select aa.id, aa.course_id, aa.comments, aa.curr_cont_t_id, aa.date_done, ' ||
'aa.unit_id, aa.time_seq, aa.week_num, bb.module_id, bb.expected_hrs, ' ||
'bb.title unit_title, cc.module_name, cc.tally_hours, cc.time_of_day, ' ||
'bb.file_upload_expected, aa.app_files_id, xx.facility_id ' ||
'from course_content aa ' ||
'left outer join units bb on aa.unit_id = bb.id ' ||
'left outer join module_categories cc on bb.module_id = cc.id ' ||
'left outer join courses xx on aa.course_id = xx.id ' ||
'where xx.facility_id = any(''{' || $1 || '}'') '
using p_facilities;
我检查了 p_facilities 以确保它不为空或为空。我什至专门将 p_facilities 设置为函数内部的一个值,如下所示:
p_facilities text = '3';
返回的错误是一致的:'EXECUTE 的查询字符串参数为空(SQL 状态 22004)'
【问题讨论】:
-
你为什么要使用动态 SQL?只需
return (select … where xx.facility_id = any(string_to_array(p_facilities, ',')));。
标签: postgresql function parameter-passing