我正在修补我从另一个SO问题中重新使用的SQL片段:
CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
DECLARE
target RECORD;
statement TEXT;
BEGIN
FOR target IN
SELECT DISTINCT table_name
FROM information_schema.columns
WHERE table_schema='public'
AND position('_' in table_name) <> 1
ORDER BY 1
LOOP
statement := 'COPY '
|| target
|| ' TO '''
|| path
|| '/'
|| target
|| '.csv'
||''' DELIMITER '';'' CSV HEADER';
EXECUTE statement;
END LOOP;
return;
end;
$$ LANGUAGE plpgsql;
它几乎可以正常工作,但它尝试执行的语句如下:
COPY (Bok_F_Acc) TO '/Users/jgt/dir/(Bok_F_Acc).csv' DELIMIT...
...虽然我希望声明如下:
COPY Bok_F_Acc TO '/Users/jgt/dir/Bok_F_Acc.csv' DELIMIT...
如何剥离这些括号?
答案 0 :(得分:2)
SELECT QUOTE_IDENT(TRIM(BOTH '()' FROM target));
请注意,这会删除任何数量的前导/尾随括号 要最多删除其中一个,您可以使用:
SELECT QUOTE_IDENT(REGEXP_REPLACE(target, E'\^\\(|\\)\$', '', 'g'));