【发布时间】:2019-12-31 08:47:05
【问题描述】:
我有一张表,用于保存企业内部进行的交易记录。
现在我正在编写一个返回在表中找到的事务 ID 的函数。以下是函数代码:
CREATE OR REPLACE FUNCTION searchList(inputList varchar) RETURNS character varying AS $BODY$
DECLARE
newList varchar;
rowcount integer;
splitted varchar[];
BEGIN
splitted = regexp_split_to_array(inputList,','); --splits the string by comma as a delimiter and produces an array as a result
select incidents.transaction_id into newList from incidents where transaction_id IN(select unnest(splitted)); --unnest function expands the array and is replaced by all the values of array
GET DIAGNOSTICS rowcount = ROW_COUNT; -- ROW_COUNT is the literal which is gonna provide the number of rows returned by previous query
IF rowcount = 0 THEN
return 'Match does not exist';
ELSE
return newList;
END IF;
END;
$BODY$ LANGUAGE plpgsql;
我提供一个字符串作为函数的输入,然后将该字符串拆分为一个数组,然后我运行一个选择查询,该查询旨在将所有此类记录的事务 id 存储到一个其事务 id 为的变量中存在于数组中,最后返回变量
但是,当我执行此函数时,即使提供的所有 transaction_id 都是匹配的,我也只会得到一个 transaction_id 作为输出。
我正在使用 select into 将结果存储到变量中,但是我感觉它只存储列的一个极值(第一个/最后一个)
有什么方法可以将可能包含多个条目的单个列的结果存储到变量中。
我知道这里已经有多个与 select into 相关的线程,但是它们都没有帮助。我最近开始使用 pgsql,所以对这个主题知之甚少
【问题讨论】:
-
@a_horse_with_no_name 我的不好,它是 9.4,即使那是古老的,但这与我们正在使用的应用程序捆绑在一起
标签: plpgsql postgresql-9.4 select-into