我正在尝试执行下面的代码。我需要这个,因为我想将表名作为参数传递(character varying
):
EXECUTE 'CREATE TABLE '||tmp_table||' AS SELECT study,
'||unnest(columns)||' AS variable_id, '||unnest_table(tblName)||
' AS variable_value FROM '||tblName;
但是,我遇到了以下错误:
函数和运算符最多可以使用一组参数
关于如何解决这个问题的任何想法?
以下是unnest_table函数的代码:
CREATE OR REPLACE FUNCTION unnest_table(_tbl text)
RETURNS SETOF text LANGUAGE plpgsql AS $func$
BEGIN
RETURN QUERY EXECUTE '
SELECT unnest(ARRAY[' || (
SELECT string_agg(a.attname || '::text', ',' ORDER BY a.attnum)
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = _tbl::regclass
AND a.attnum > 0
AND a.attisdropped = false
) || '])
FROM ' || _tbl::regclass;
END
$func$;
答案 0 :(得分:2)
那是因为unnest
&您的unnest_table
返回SETOF <sometype>
和operators can take at most one set argument
,所以f.ex。:
SELECT unnest(ARRAY['a', 'b', 'c']);
-- will return
unnest
------
"a"
"b"
"c"
SELECT unnest(ARRAY['a', 'b', 'c']) || 'd';
-- will return
?column?
--------
"ad"
"bd"
"cd"
SELECT unnest(ARRAY['a', 'b', 'c']) || 'd' || unnest(ARRAY['a', 'b', 'c']);
-- will return
ERROR: functions and operators can take at most one set argument
SQL state: 0A000
修改:但我非常怀疑,您想要创建具有相同名称的更多表格 - 同时EXECUTE
不接受多行:
ERROR: query "..." returned more than one row
SQL state: 21000
我认为你应该使用类似array_to_string()
函数的东西。