Select from a table variable

后端 未结 1 1010
天涯浪人
天涯浪人 2020-12-03 19:40

I am trying to save the result of a SELECT query, pass it, and reuse it in another PL/pgSQL function:

DECLARE
  table_holder my_table; --the typ         


        
相关标签:
1条回答
  • 2020-12-03 19:43

    There are no "table variables" in plpgsql. That's something you would find in SQL Server.

    Use a temporary table instead:

    BEGIN
    
    CREATE TEMP TABLE table_holder AS
    SELECT * FROM table_holder
    WHERE <some condition>
    ORDER BY <some expression>
    ;
    
    ...
    
    END
    

    A temporary table exists for the lifetime of a session. To drop it at the end of the function (or an enclosing transaction) automatically, use ON COMMIT DROP in the creating statement.

    CREATE TEMP TABLE table_holder ON COMMIT DROP AS
    SELECT ...
    

    The temporary table is automatically visible to any other function in the same session.

    One alternative would be to use cursors in plpgsql.

    0 讨论(0)
提交回复
热议问题