Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:19:51+00:00 2026-06-07T13:19:51+00:00

I have a stored procedure in PostgreSQL 8.4 that calls another stored procedure depending

  • 0

I have a stored procedure in PostgreSQL 8.4 that calls another stored procedure depending on the integer value passed in as a parameter. Those stored procedures are called such that they should return a relation with one integer column. The problem I am having is that the outer stored procedure always returns a relation with the correct number of rows but with all of the id’s NULL.

Here is the stored procedure reduced to its simplest form:

CREATE OR REPLACE FUNCTION spa(count integer) 
RETURNS TABLE (id integer) AS $$
BEGIN
    RETURN QUERY SELECT generate_series(1, count);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION spb(count integer) 
RETURNS TABLE (id integer) AS $$
BEGIN
    RETURN QUERY SELECT generate_series(1, count);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION conditional_relation_return(objectType integer, count integer) 
RETURNS TABLE (id integer) AS $$
BEGIN
    IF objectType = 1 THEN
        RETURN QUERY SELECT id FROM spa(count);
    ELSIF objectType = 2 OR objectType = 3 THEN
        RETURN QUERY SELECT id FROM spb(count);
    END IF;

END;
$$ LANGUAGE plpgsql;

And if you call it:

# select * from conditional_relation_return(1, 2);
 id 
----


(2 rows)

Or more specifically:

# select count(*) from conditional_relation_return(1, 2) where id is null;
 count 
-------
     2
(1 row)

But if you call one of the referenced stored procedures, you get the correct results:

# select * from spa(2);
 id 
----
  1
  2
(2 rows)

So why does conditional_relation_return return all NULLs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:19:52+00:00Added an answer on June 7, 2026 at 1:19 pm

    The id of spa conflicts with the out parameter id (RETURNS TABLE (id integer)). Postgresql 8.4 doesn’t complain, it chooses id from out parameter id instead of the saner one(id of spa).

    Postgresql 9.1’s complain on your original code:

    ERROR:  column reference "id" is ambiguous
    LINE 1: SELECT id FROM spa(count)
                   ^
    DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
    QUERY:  SELECT id FROM spa(count)
    CONTEXT:  PL/pgSQL function "conditional_relation_return" line 4 at RETURN QUERY
    

    To fix it, fully qualify the id on your query:

    CREATE OR REPLACE FUNCTION conditional_relation_return(
        objectType integer, count integer) 
    RETURNS TABLE (id integer) AS $$
    BEGIN
        IF objectType = 1 THEN
            RETURN QUERY SELECT x.id FROM spa(count) as x;
        ELSIF objectType = 2 OR objectType = 3 THEN
            RETURN QUERY SELECT x.id FROM spb(count) as x;
        END IF;
    
    END;
    $$ LANGUAGE plpgsql;
    

    Output:

    test=# select * from conditional_relation_return(1, 2);
     id 
    ----
      1
      2
    (2 rows)
    

    Postgresql honors the column(s) name you choose from your RETURNS TABLE. It still slot x.id to the id of your RETURNS TABLE. So, even you decided to rename your RETURNS TABLE return column’s name, it will still slot x.id to that name, e.g.

    CREATE OR REPLACE FUNCTION conditional_relation_return(
        objectType integer, count integer) 
    RETURNS TABLE (hahah integer) AS $$
    BEGIN
        IF objectType = 1 THEN
            RETURN QUERY SELECT x.id FROM spa(count) as x;
        ELSIF objectType = 2 OR objectType = 3 THEN
            RETURN QUERY SELECT x.id FROM spb(count) as x;
        END IF;
    
    END;
    $$ LANGUAGE plpgsql;
    

    Output:

    test=# select * from conditional_relation_return(1, 2);
     hahah 
    -------
         1
         2
    (2 rows)
    

    Notice the hahah column

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stored procedure that calls another stored procedure. Let's call them OUTER and
I have a stored procedure that has the parameter: @desk VARCHAR(50) I want to
I have heard that one in PostgreSQL can write stored procedures in Ruby. But
I have heard that one in PostgreSQL could write stored procedures in ruby (also
I have stored procedure that insanely times out every single time it's called from
I have a stored procedure that takes a single parameter of data-type XML. I
I have a stored procedure that has an OUT parameter of type SYS_REFCUROR. From
I have a stored procedure in PostgreSQL that takes a t_document composite type defined
I have stored procedures in SQL Server T-SQL that are called from .NET within
I have this stored procedure that I want to use for my SAP crystal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.