我正在尝试过滤具有产品 ID 作为外键的行的 postgresql 表。对于每个产品 id,我需要将每个 1 个 csv 导出到一个文件夹,例如 prod1.csv、prod2.csv 等。我试图创建下面的函数来自动执行此操作,但是当我运行它时该函数失败了。任何人都可以帮助我修复功能或推荐更好的方法吗?
CREATE or replace FUNCTION exportdata()
RETURNS SETOF record AS
$$
DECLARE
rec text;
BEGIN
FOR rec IN
(
Select distinct t.products from trndailyprices as t --Get the list of products in the table
)
LOOP
Copy (
Select * from trndailyprices as t
where t.products = rec ---1st record is product1
order by t.effectivedate)
To 'C:/testQ/' ||rec || '.csv' With CSV;---expected file is product1.csv for 1st record
END LOOP;
END;
$$ LANGUAGE plpgsql;