在函数中的 postgresql 副本中使用变量时列不存在错误

column does not exist error when using a variable in postgresql copy in function

我有这个 postgresql 函数

CREATE OR REPLACE FUNCTION sp_select_test(_id bigint)
  RETURNS void AS
$BODY$                

BEGIN
        copy (select sub.id
        from (
          SELECT id FROM sim s where id = _id) sub) TO '/tmp/results.tsv';
END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_select_test(bigint)
OWNER TO postgres;

当我运行

select * from sp_select_test(264);

我收到错误 - “_id”列不存在

如果我将函数中的变量 _id 替换为值 264,该函数将起作用。也就是说,如果我使用 id = 264

而不是 id = _id

函数失败的任何原因?

CREATE OR REPLACE FUNCTION sp_select_test(_id bigint)
  RETURNS void AS
$BODY$                

BEGIN
        execute $$copy (select sub.id
        from (
          SELECT id FROM sim s where id = $$||_id||$$) sub) TO '/tmp/results.tsv'$$;
END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_select_test(bigint)
OWNER TO postgres;