Search code examples
postgresql

How could I return different set of records from the same function in postgreSQL?


I am working on migrating our SQL Server databases to postgreSQL. One of our existing Stored Procedure looks like this (very simplified):

CREATE PROCEDURE [dbo].[GetMyData]
    @StartTime As DateTime,
    @EndTime As DateTime,
    @GetResultCount BIT = 0
AS
BEGIN
    IF (@GetResultCount = 1)
    BEGIN
        SELECT COUNT(*) FROM myTable where xDate >= @StartTime and xDate < @EndTime;
        RETURN;
    END
    SELECT x,y,z FROM myTable where xDate >= @StartTime and xDate < @EndTime;
END;

Is there an easy way to convert this, or if not, how would you suggest to convert this? Thanks in advance


Solution

  • Adapted from this SO answer

    If you view the above answer, look at the anyelement placeholder type. You have to pass one more parameter to the function to tell it what type you expect to be returned. A sample is below, but note that I already had a table named test in my database. Postgres exposes a type of the same name as my table, which I used in the second example call.

    create or replace function sample_function_two_return_types
    (
        __return_type anyelement,
        __return_count bool
    )
    returns setof anyelement
    language 'plpgsql'
    as $$
    begin
        if (__return_count = true) then
            return query select count(*) c from test;
        else
            -- NOTE: this return statement needed to be in an else block,
            -- or else postgres thinks it's adding it to all the other return
            -- responses.
            return query select * from test;    
        end if;
        
    end
    $$;
    
    -- call the function with the bigint type to get the count. (if
    -- you want an int, you'll need to cast it in the function or 
    -- after it returns)
    select * from sample_function_two_return_types(null::bigint, true);
    
    -- call the function with the return table's type, so it knows you expect
    -- records of that table's field list returned.
    select * from sample_function_two_return_types(null::test, false);