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 7782459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:33:04+00:00 2026-06-01T19:33:04+00:00

I have dynamicly generated SELECT. I try to return result as SETOF RECORD. Sth

  • 0

I have dynamicly generated SELECT. I try to return result as SETOF RECORD. Sth like that:

CREATE FUNCTION test(column_name text) RETURNS SETOF RECORD AS $$
DECLARE
   row  RECORD;
BEGIN
   FOR row IN EXECUTE 'SELECT ' || quote_ident(column_name) || ' FROM dates'
LOOP
   RETURN NEXT row;
END LOOP;
RETURN;
END;
$$ LANGUAGE 'plpgsql';

When I try:

SELECT * FROM test('column1');

I get this:

ERROR:  a column definition list is required for functions returning "record"

I know that column1 is integer type:

SELECT * FROM test('column1') f(a int);

result is correct, because I know that this is going to be Integer type.

When I try:

SELECT * FROM test('column1') f(a varchar);

I get error:

ERROR:  wrong record type supplied in RETURN NEXT
DETAIL:  Returned type integer does not match expected type character varying in column 1.

Now my question:
What to do to get rid of part of querty where I define types ‘f(a int)’. It should by feasible because Postgres knowns what is returned type. I tried with IMMUTABLE options, but unsuccessfully.

  • 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-01T19:33:05+00:00Added an answer on June 1, 2026 at 7:33 pm

    You could cast the value to text inside the function, and declare that the function RETURNS SETOF text. You can also return the whole result set at once; no need to iterate explicitly.

    CREATE TABLE dates (column1 int, column2 date);
    INSERT INTO dates VALUES (1, date '2012-12-22'), (2, date '2013-01-01');
    
    CREATE FUNCTION test(column_name text) RETURNS SETOF text AS $$
    BEGIN
       RETURN QUERY EXECUTE 'SELECT '
          || quote_ident(column_name) || '::text FROM dates';
    END;
    $$ LANGUAGE 'plpgsql';
    

    Now SELECT test('column1'); yields:

     test 
    ------
     1
     2
    (2 rows)
    

    … and (with my locale settings) SELECT test('column2'); yields:

        test    
    ------------
     2012-12-22
     2013-01-01
    (2 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have a dynamically generated rss feed that is about 150M in size (don't
I have a series of dynamically generated inputs that I need to have ajax
I have a form that is dynamically generated, and has dynamically generated id's (and
I have a grid that is dynamically generated based on search criteria. I render
I have a table of data that is generated dynamically based on the contents
I have a form selection that allows users to select a product from a
I have a set of buttons that, when clicked, should select the first text
I have a button on a form that is dynamically spawning a select list,
Is it possible to have checkboxes with a gridview that is dynamically generated from

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.