Sarfaraz Makandar
Sarfaraz Makandar

Reputation: 6733

PostgreSQL:How to return result of a SELECT statement within a function using PostgreSQL?

Here i have following function, but not getting how to return the result.

create or replace function f1() returns void as  
$body$  
begin    
    SELECT "Fname",  
        "Lname",   
        count("City" = 'A-B' OR NULL) AS "A-B",  
        count("City" = 'C-D' OR NULL) AS "C-D",  
    FROM "Table1"  
    WHERE  "City" in ('A-B','C-D')  
    GROUP BY 1,2  
    ORDER BY 1,2  

Upvotes: 0

Views: 113

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324295

Change the return type to RETURNS TABLE (fieldname type, fieldname type, ...) and use RETURN QUERY SELECT ....

See the PL/PgSQL docs.

Upvotes: 2

Related Questions