我是Posgresql的新手
我有一个表函数:
CREATE OR REPLACE FUNCTION stage.get_primary_key_info(
schemaName text,
tableName text
) RETURNS TABLE(constraint_name text, column_name text) AS
$BODY$
SELECT c.constraint_name, c.column_name
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
ON t.constraint_name = c.constraint_name
WHERE t.table_schema = schemaName
AND t.table_name = tableName
AND t.constraint_type = 'PRIMARY KEY'
;
$BODY$ LANGUAGE sql;
我正在尝试使用此功能,如:
FOR c IN (SELECT * FROM stage.get_primary_key_info(target_schema, stmt.tablename))
LOOP
joinFields = joinFields || FORMAT('t.%s = s.%s AND', c.column_name);
END LOOP;
但我有这个错误:
元组的循环变量必须是记录类型的变量 或元组或标量变量列表
答案 0 :(得分:0)
尝试将c
声明为record
。您还缺少FORMAT
字符串中的第二个参数。
另外,请查看string_agg,您可以完全跳过该循环。这是一个例子(用y
代替第二个参数):
SELECT string_agg(FORMAT('t.%s = s.%s', column_name, 'y'), ' AND ')
FROM get_primary_key_info(target_schema, stmt.tablename)
;