我的 postgres 函数有问题。我创建了一个将数据从 db 复制到 csv 的函数。
我尝试在工作台上运行该函数并且没有错误,但是当我运行测试时出现美元报价错误。
这是功能:
--------- the @DELIMITER / is necessary in order to sent sql statement
with mybatis...
-- @DELIMITER /
/
CREATE OR REPLACE FUNCTION save_audit_deletions(IN days numeric, IN table_name text)
RETURNS void AS
$body$
DECLARE
interval INT;
statement_copy text;
statement_count text;
copied_rows INTEGER; --number of rows copied by COPY
backup_rows INTEGER; --number of rows that COPY needs to copy into csv
BEGIN
UPDATE table_name SET backup = 1 WHERE backup = 0 AND creationdate >= now()::DATE - days AND creationdate < now()::DATE;
statement_copy := 'COPY (SELECT * FROM table_name WHERE backup = 1) TO ''/var/audiobays/logs/audit/' || table_name || '_deletions_(' || date-days|| ').csv'' CSV DELIMITER '','' HEADER;';
execute statement_copy
into copied_rows;
statement_count := 'SELECT COUNT (*) FROM table_name WHERE backup = 1';
execute statement_count
into backup_rows;
IF copied_rows = backup_rows THEN
DELETE FROM table_name WHERE backup = 1;
ELSE
UPDATE table_name SET backup = 0 WHERE backup = 1;
END IF;
END
$body$
LANGUAGE plpgsql;
/
-- @DELIMITER ;
这就是错误。
Error executing: CREATE OR REPLACE FUNCTION save_audit_deletions(IN days numeric, IN table_name text)
RETURNS void AS
$body$
DECLARE
interval INT;
statement_copy text;
statement_count text;
copied_rows INTEGER; --number of rows copied by COPY
backup_rows INTEGER; --number of rows that COPY needs to copy into csv
BEGIN
UPDATE table_name SET backup = 1 WHERE backup = 0 AND creationdate >= now()::DATE - days AND creationdate < now()::DATE;
statement_copy := 'COPY (SELECT * FROM table_name WHERE backup = 1) TO ''/var/audiobays/logs/audit
. Cause: org.postgresql.util.PSQLException: Unterminated dollar quote started at position 104 in SQL CREATE OR REPLACE FUNCTION
. Expected terminating $$
Error executing: execute statement_copy
. Cause: org.postgresql.util.PSQLException: Unterminated dollar quote started at position 328 in SQL execute statement_copy
. Expected terminating $$