我在sql中创建了一个函数,该函数调用非常大的表的单独块。
我添加了一个列chunk
,其值介于0到4之间(int
类型的列),并希望将该函数逐块应用于表。
这是我要参数化的sql:
update some_t t
set col_2 = (select t2.col_2 from some_t t2
where t2.col_1 = t. col_1
and t2.col_2 is not null
limit 1)
where t.col_2 is null;
这是带有附加参数chunk
的函数,它使我可以将函数划分到表的各个块上。
CREATE FUNCTION impute_place(table_ text, impute_from_col text, impute_to_col text, chunk text)
RETURNS VOID
AS $func$
BEGIN
EXECUTE 'UPDATE ' || table_ || ' t1
SET ' || impute_to_col || ' = (select t2.' || impute_to_col ||
' from ' || table_ || ' t2
where t2.' || impute_from_col || ' = t1.' || impute_from_col ||
' and t2.' || impute_to_col || ' is not null
limit 1)
WHERE t1.' || impute_to_col || ' is null
AND chunk = ' || chunk ||;
END;
$func$
LANGUAGE plpgsql VOLATILE;
当我尝试随后在所有分区上运行它时,出现错误,我试图修复但无法修复。
此外,如果没有AND chunk = ' || chunk ||;
功能就无法正常工作,因此错误必须在这一行中!
select impute_place('some_t', 'col_1', 'col_2', '0');
select impute_place('some_t', 'col_1', 'col_2', '1');
select impute_place('some_t', 'col_1', 'col_2', '2');
select impute_place('some_t', 'col_1', 'col_2', '3');
select impute_place('some_t', 'col_1', 'col_2', '4');
我收到以下错误:
[42883] ERROR: operator does not exist:
text || Hint: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
Where: PL/pgSQL function impute_place(text,text,text,text) line 4 at EXECUTE