当我运行此功能时:
CREATE OR REPLACE FUNCTION insert_styles(raw_styles text)
RETURNS integer AS
$BODY$
declare
arr_value TEXT[];
upper_limit INTEGER;
style_ids INTEGER[];
BEGIN
arr_value := string_to_array(raw_styles, ',');
upper_limit := array_upper(arr_value, 1);
RAISE NOTICE 'arr_value = %', arr_value;
SELECT music_style FROM music_style INTO style_ids WHERE name = ANY (arr_value);
RETURN upper;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
使用此查询:
SELECT insert_styles('Techno,ddd,wer,WJKDF');
它给了我以下错误:
NOTICE: arr_value = {Techno,ddd,wer,WJKDF}
ERROR: array value must start with "{" or dimension information
CONTEXT: PL/pgSQL function "insert_styles" line 10 at SQL statement
我无法弄明白。我是一个postgres新手!
再见!!
答案 0 :(得分:2)
假设您的music_style
列具有整数数据类型,您只需将array_agg
聚合添加到查询中:
SELECT array_agg(music_style)
FROM music_style
INTO style_ids -- style_ids is integer[]
WHERE name = ANY (arr_value);