0

我需要构建一个存储过程,它需要输入一个 varchars 数组。它将使用类似的语法搜索这些

SELECT *
FROM mytable
WHERE search_index @@ to_tsquery(' ');

如果我像 tom,dick,harry 这样的过程输入输入,则查询应该像动态构建一样

SELECT *
FROM mytable
WHERE search_index @@ to_tsquery('tom | dick | harry ');


CREATE OR REPLACE FUNCTION text_search(search_terms character varying[], ent_id bigint)
  RETURNS entity_base AS

$BODY$DECLARE

  result_data entity_base;

  search_data text;
BEGIN

     search_data := array_to_string('search_terms[]', '|');

     SELECT * INTO result_data FROM entity_base WHERE search_index @@ to_tsquery(search_data) and entity_id = ent_id;
     RETURN result_data;
END;$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;

当我运行这个我得到错误

ERROR:  could not determine polymorphic type because input has type "unknown"
CONTEXT:  SQL statement "SELECT  array_to_string('search_terms[]', '|')"
PL/pgSQL function "text_search" line 5 at assignment

********** Error **********

ERROR: could not determine polymorphic type because input has type "unknown"
SQL state: 42804
Context: SQL statement "SELECT  array_to_string('search_terms[]', '|')"
PL/pgSQL function "text_search" line 5 at assignment
4

1 回答 1

1

使用array_to_string ()。

于 2010-06-04T05:30:41.323 回答