Postgresql 我需要捕获所有产品标识,以便在sql in子句中使用它
多行。将条件直接放在Postgresql 我需要捕获所有产品标识,以便在sql in子句中使用它,postgresql,plpgsql,Postgresql,Plpgsql,多行。将条件直接放在EXECUTE中。出于显示目的,使用STRING\u AGG显示逗号分隔的产品标识 CREATE OR replace FUNCTION testfunction(timestamp) returns void AS $body$ DECLARE product_ids text; BEGIN -- this is returning multiple rows, but it assigning only one to prodcu
EXECUTE
中。出于显示目的,使用STRING\u AGG
显示逗号分隔的产品标识
CREATE OR replace FUNCTION testfunction(timestamp) returns void
AS
$body$
DECLARE
product_ids text;
BEGIN
-- this is returning multiple rows, but it assigning only one to prodcut_ids variable.
SELECT DISTINCT product_id AS product_id
INTO product_ids
FROM test_product
WHERE created_on > $1::timestamp
ORDER BY product_id ;
RAISE notice 'product IDs : %', product_ids;
EXECUTE 'copy (SELECT * FROM test_product WHERE product_id in ('
|| product_ids
|| ' ) ) TO ''C:\projects\test_product.csv'' CSV HEADER';
END $body$ LANGUAGE plpgsql volatile;
-- it is only exporting one record even the above select returning
我是这个平台的新手。需要学习如何正确发布问题。哇,你救了我的命。我所做的是创建一个临时表-->插入>,然后从中获取ID。非常感谢。
CREATE OR replace FUNCTION testfunction ( timestamp ) returns void
AS
$body$
DECLARE
v_created_on timestamp := $1;
BEGIN
SELECT STRING_AGG( product_id,',' ORDER BY product_id)
INTO product_ids
FROM test_product
WHERE created_on > v_created_on;
RAISE notice 'product IDs : %', product_ids;
EXECUTE 'copy ( SELECT * FROM test_product WHERE created_on > $1 )
TO ''C:\projects\test_product.csv'' CSV HEADER' USING v_created_on;
END;
$body$ LANGUAGE plpgsql volatile;