Cách truyền array parameter cho stored procedure trong postgreSQL

Đây là ví dụ để bạn có thể truyền parameter kiểu mảng vào  stored procedure trong postgreSQL

Ví dụ ta có stored procedure

CREATE OR REPLACE FUNCTION example_array_input(INT[]) RETURNS SETOF ids AS
$BODY$
DECLARE
in_clause ALIAS FOR $1;
clause TEXT;
rec RECORD;
BEGIN
FOR rec IN SELECT id FROM ids WHERE id = ANY(in_clause)
LOOP
RETURN NEXT rec;
END LOOP;
-- final return
RETURN;
END
$BODY$ language plpgsql;

Cách truy vấn như sau
SELECT * FROM example_array_input('{1,2,4,5,6}'::INT[]);

Cách truyền tham số trên code C#

new DataElement("@par", ArrayValue, NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Integer)

Leave a Reply

Your email address will not be published.