PostgreSQl function return multiple dynamic result sets

我与影子孤独终老i 提交于 2021-01-28 06:26:27

问题


I have an old MSSQL procedure that needs to be ported to a PostgreSQL function. Basically the SQL procedure consist in a CURSOR over a select statement. For each cursor entity i have three select statements based on the current cursor output.

FETCH NEXT FROM @cursor INTO @entityId
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT * FROM table1 WHERE col1 = @entityId
SELECT * FROM table2 WHERE col2 = @entityId
SELECT * FROM table3 WHERE col3 = @entityId
END

The tables from the SELECT statements have different columns.

I know that the PostgreSQL use refcursor in order to return multiple result sets but the question is if is possible to open and return multiple dynamic refcursors inside of a loop?

The Npgsql .NET data provider is used for handling the results.

Postgres test code with only 1 cursor inside loop:

CREATE OR REPLACE FUNCTION "TestCursor"(refcursor) 
RETURNS SETOF refcursor AS
$BODY$

DECLARE
    entity_id integer;  
BEGIN

    FOR entity_id IN SELECT "FolderID" from "Folder"
    LOOP
        OPEN $1 FOR SELECT * FROM "FolderInfo" WHERE "FolderID" = entity_id;
        RETURN NEXT $1;
        CLOSE $1;   
    END LOOP;
END;

$BODY$
LANGUAGE 'plpgsql' VOLATILE;

Then the test code:

BEGIN;
SELECT * FROM "TestCursor"('c');
FETCH ALL IN c;
COMMIT;

The SELECT * FROM "TestCursor"('c'); output is like on screenshot: Then when i try to fetch data i get the error: ERROR: cursor "c" does not exist


回答1:


You can emulate it via SETOF refcursor. But it is not good idea. This T-SQL pattern is not supported well in Postgres, and should be prohibited when it is possible. PostgreSQL support functions - function can return scalar, vector or relation. That is all. Usually in 90% is possible to rewrite T-SQL procedures to clean PostgreSQL functions.



来源:https://stackoverflow.com/questions/33494520/postgresql-function-return-multiple-dynamic-result-sets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!