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

  • SEARCH
  • Home
  • 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 9254177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:24:06+00:00 2026-06-18T11:24:06+00:00

I need a simple function to return dynamic set of columns. I’ve found couple

  • 0

I need a simple function to return dynamic set of columns. I’ve found couple of examples on SO and end up with the following:

CREATE or replace FUNCTION getColumn(_column1 text, _column2 text, _column3 text, _table text)
  RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text) AS $$
BEGIN
    RETURN QUERY EXECUTE 
        'SELECT ' 
            || quote_ident(_column1)::text || ' as cmf1,'
            || quote_ident(_column2)::text || ' as cmf2,'
            || quote_ident(_column3)::text || ' as cmf3'
        ' FROM '
            || quote_ident(_table); 
END;
 $$ LANGUAGE plpgsql;

I need this function to work only with varchar/text columns so I created this testing table:

create table test20130205 (
    a text,
    b text,
    c varchar,
    d text)
;

Finally, I can run some tests:

select * from getColumn('a','b','d','test20130205');
-- ok
select * from getColumn('a','b','c','test20130205');
-- error
ERROR:  structure of query does not match function result type
DETAIL:  Returned type character varying does not match expected type text in column 3.
CONTEXT:  PL/pgSQL function getcolumn(text,text,text,text) line 3 at RETURN QUERY

It seems like type for column c (varchar) is checked before cast – this seems strange, but I guess I’ve missed something.

How can I fix my function?

(PostgreSQL 9.1)

  • 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-18T11:24:08+00:00Added an answer on June 18, 2026 at 11:24 am

    In your current function, the casts to text do not apply to the output columns values, they apply to their names (the result of quote_ident).

    The cast should be moved inside the query itself:

    CREATE or replace FUNCTION getColumn(_column1 text, _column2 text, _column3 text, _table text)
      RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text) AS $$
    BEGIN
        RETURN QUERY EXECUTE 
            'SELECT ' 
                || quote_ident(_column1) || '::text as cmf1,'
                || quote_ident(_column2) || '::text as cmf2,'
                || quote_ident(_column3) || '::text as cmf3'
            ' FROM '
                || quote_ident(_table); 
    END;
     $$ LANGUAGE plpgsql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to change HTML background with JavaScript Function? I need some simple function to
So all I need is a simple function that sets it up (eating incoming
I need a simple proxy PHP function/script that can download a file from a
I have HTML + CSS text. I need some lib with simple function like
I need to parse a simple statement (essentially a chain of function calls on
How do I create unique key value in PHP? I need simple unique keys
I need to implement simple function that is called from multiple threads. The logic
I am NOT looking for a plug-in, just need simple jQuery function with regexp.
I need to write a very simple function in SML. It should check if
I need a simple function that writes an array of files to one zip

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.