在Postgresql代码(版本10)中,我需要在表中插入从记录变量中提取的行,但是表名存储在function参数中,因此列是动态的。
execute format('INSERT INTO %I.%I SELECT ($1).*' , schema_name, table_name_sync)
USING source_row;
CREATE OR REPLACE FUNCTION syncTable(table_name_sync VARCHAR)
RETURNS void AS $$
DECLARE
--Cursors declaration
source_rows refcursor;
operator_to_apply VARCHAR;
--Variables declaration
source_row RECORD;
schema_name VARCHAR;
table_name_tmp_schema VARCHAR;
BEGIN
schema_name := 'myschema';
table_name_tmp_schema := 'myschema.tmp_' || table_name_sync;
--Open cursors
OPEN source_rows FOR EXECUTE 'SELECT * FROM ' || table_name_tmp_schema;
LOOP
FETCH source_rows INTO source_row;
operator_to_apply := eval_operator(...);
IF (operator_to_apply = 'INSERT')
THEN
execute format('INSERT INTO %I.%I SELECT ($1).*' , schema_name, table_name_sync)
USING source_row;
ELSIF (operator_to_apply = 'UPDATE')
THEN
...
END IF;
EXIT WHEN NOT FOUND;
END LOOP;
END; $$
LANGUAGE plpgsql;
当我调用syncTable('mytable')时,出现以下错误:
«错误:记录类型尚未注册 上下文:SQL语句“ INSERT INTO myschema.mytable SELECT($ 1)。*””