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
多行。
答案 0 :(得分:1)
将条件直接放在EXECUTE
中。出于显示目的,请使用STRING_AGG
显示逗号分隔的product_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;
答案 1 :(得分:0)
在其他情况下,不存在抛出错误的错误运算符:bigint = text,42883
对于将单个值转换为文本时,它可以正常工作。在(inputProduct_ids)中的product_id :: text中;
创建函数ourFunction(text){ inputProduct_ids文本:= $ 1;
选择STRING_AGG(product_id :: TEXT,','ORDER BY product_id)
INTO product_id
来自fdp_product
在('|| inputProduct_ids ||')中的product_id;
///
//
}
选择ourFunction('573,574,575,576,579,580,581,584');