我有一个在postgres UDF中生成的查询字符串,我想将它的结果放在临时表中以执行连接(我使用LIMIT和OFFSET,我不想加入其他ttables只是为了结束最后将数据关闭 – 也就是查询计划中的LIMIT运算符.我尝试使用以下语句创建临时表.
CREATE LOCAL TEMP TABLE query_result ON COMMIT DROP AS EXECUTE query_string_;
********** Error ********** ERROR: prepared statement "query_string_" does not exist sql state: 26000 Context: sql statement "CREATE LOCAL TEMP TABLE query_result ON COMMIT DROP AS EXECUTE query_string_" PL/pgsql function "search_posts_unjoined" line 48 at sql statement
另外,我试过准备政治家,但我也无法正确使用语法.
有问题的UDF是:
CREATE OR REPLACE FUNCTION search_posts_unjoined( forum_id_ INTEGER,query_ CHaraCTER varying,offset_ INTEGER DEFAULT NULL,limit_ INTEGER DEFAULT NULL,from_date_ TIMESTAMP WITHOUT TIME ZONE DEFAULT NULL,to_date_ TIMESTAMP WITHOUT TIME ZONE DEFAULT NULL,in_categories_ INTEGER[] DEFAULT '{}' ) RETURNS SetoF forum_posts AS $$ DECLARE join_string CHaraCTER varying := ' '; from_where_date CHaraCTER varying := ' '; to_where_date CHaraCTER varying := ' '; query_string_ CHaraCTER varying := ' '; offset_str_ CHaraCTER varying := ' '; limit_str_ CHaraCTER varying := ' '; BEGIN IF NOT from_date_ IS NULL THEN from_where_date := ' AND fp.posted_at > ''' || from_date_ || ''''; END IF; IF NOT to_date_ IS NULL THEN to_where_date := ' AND fp.posted_at < ''' || to_date_ || ''''; END IF; IF NOT offset_ IS NULL THEN offset_str_ := ' OFFSET ' || offset_; END IF; IF NOT limit_ IS NULL THEN limit_str_ := ' LIMIT ' || limit_; END IF; IF NOT limit_ IS NULL THEN END IF; CREATE LOCAL TEMP TABLE un_cat(id) ON COMMIT DROP AS (select * from unnest(in_categories_)) ; if in_categories_ != '{}' THEN join_string := ' INNER JOIN un_cat uc ON uc.id = fp.category_id ' ; END IF; query_string_ := ' SELECT fp.* FROM forum_posts fp' || join_string || 'WHERE fp.forum_id = ' || forum_id_ || ' AND to_tsvector(''english'',fp.post_text) @@ to_tsquery(''english'','''|| query_||''')' || from_where_date || to_where_date || offset_str_ || limit_str_ || ';'; CREATE LOCAL TEMP TABLE query_result ON COMMIT DROP AS EXECUTE query_string_; RAISE NOTICE '%',query_string_; RETURN QUERY EXECUTE query_string_; END; $$LANGUAGE plpgsql;
当删除相关语句时,它可以正常工作.
解决方法
改为使用:
EXECUTE ' CREATE TEMP TABLE query_result ON COMMIT DROP AS '|| query_string_;
>执行整个声明.
语法形式CREATE TABLE foo AS EXECUTE< query>无效.
> LOCAL只是一个噪音词,在此背景下被忽略.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。