我的问题是我无法在存储过程中运行 COPY 语句,我不知道为什么。
我的功能:
CREATE OR REPLACE FUNCTION copy_table(mynumber integer)
RETURNS void AS
$BODY$
DECLARE
iNumber integer := 0;
BEGIN
iNumber := mynumber;
RAISE NOTICE 'my number: %', iNumber;
IF(iNumber <> 0) THEN
COPY (SELECT * FROM mytable WHERE number = iNumber) TO '/tmp/test.csv' WITH CSV;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION copy_table(integer)
OWNER TO "owner";
现在,如果我现在这样做:
COPY (SELECT * FROM mytable WHERE number = 2) TO '/tmp/test.csv' WITH CSV
我只是得到了想要的结果,但是如果我使用前面描述的函数:
SELECT copy_table(2)
我得到错误:
NOTICE: my number: 2
ERROR: there is no parameter $1
LINE 1: COPY (SELECT * FROM mytable WHERE number = $1 ) TO '/t...
^
QUERY: COPY (SELECT * FROM mytable WHERE number = $1 ) TO '/tmp/test.csv' WITH CSV
CONTEXT: PL/pgSQL function "copy_table" line 10 at SQL statement
********** Error **********
ERROR: there is no parameter $1
SQL state: 42P02
Context: PL/pgSQL function "copy_table" line 10 at SQL statement
任何帮助将不胜感激,谢谢!